CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/packages/bsp/rk3399/scripts/test_camera-gst.sh
Views: 3960
1
#!/bin/bash
2
3
export DISPLAY=:0.0
4
#su orangepi -c "gst-launch-1.0 -vvv rkisp device=${DEV} io-mode=1 analyzer=1 enable-3a=1 path-iqf=/etc/cam_iq.xml \
5
# ! video/x-raw,format=NV12,width=1280,height=720, framerate=30/1 ! videoconvert ! autovideosink"
6
7
icam=0
8
vsnk=
9
action="preview"
10
output=
11
verbose="yes"
12
13
#----------------------------------------------------------
14
usage()
15
{
16
echo "Usage: $0 [ARGS]"
17
echo
18
echo "Options:"
19
echo -e " -h, --help\n\tshow this help message and exit"
20
echo -e " -i, --index <0|1>\n\tcamera index, 0 or 1"
21
echo -e " -a, --action <preview|photo|video>\n\tpreview, take photo or record video"
22
echo -e " -o, --output <filename>\n\toutput file name"
23
echo -e " -v, --verbose\n\tshow full command"
24
exit 1
25
}
26
27
parse_args()
28
{
29
TEMP=`getopt -o "i:a:o:v:h" --long "index:,action:,output:,verbose:,help" -n "$SELF" -- "$@"`
30
if [ $? != 0 ] ; then exit 1; fi
31
eval set -- "$TEMP"
32
33
while true; do
34
case "$1" in
35
-i|--index ) icam=$2; shift 2;;
36
-a|--action) action=$2; shift 2;;
37
-o|--output) output=$2; shift 2;;
38
-v|--verbose) verbose=$2; shift 2;;
39
-h|--help ) usage; exit 1 ;;
40
-- ) shift; break ;;
41
* ) echo "invalid option $1"; usage; return 1 ;;
42
esac
43
done
44
45
if [ -z "$output" ]; then
46
if [ $action = "photo" ]; then
47
output="Image_MIPI.jpg"
48
elif [ $action = "video" ]; then
49
output="Video_MIPI.ts"
50
fi
51
fi
52
}
53
54
isp_default_vsnk="autovideosink"
55
usbcamera_default_vsnk="autovideosink"
56
57
#----------------------------------------------------------
58
SELF=$0
59
parse_args $@
60
61
# selfpath
62
declare -a PreviewDevs=()
63
# mainpath
64
declare -a PictureDevs=()
65
# camera type
66
declare -a CameraTypes=()
67
# sink
68
declare -a Sinks=()
69
70
# preivew format
71
declare -a PreviewModes=()
72
# photo format
73
declare -a PictureModes=()
74
# video recoard format
75
declare -a VideoModes=()
76
77
# isp1
78
if [ -d /sys/class/video4linux/v4l-subdev2/device/video4linux/video1 -o \
79
-d /sys/class/video4linux/v4l-subdev5/device/video4linux/video1 ]; then
80
PreviewDevs+=("/dev/video1")
81
PictureDevs+=("/dev/video0")
82
CameraTypes+=("mipi")
83
# use did not specify sink
84
if [ -z "${vsnk}" ]; then
85
Sinks+=(${isp_default_vsnk})
86
else
87
Sinks+=(${vsnk})
88
fi
89
PreviewModes+=("width=1280,height=720,framerate=30/1")
90
PictureModes+=("width=1920,height=1080,framerate=10/1")
91
VideoModes+=("width=1280,height=720,framerate=30/1")
92
fi
93
94
# isp2
95
if [ -d /sys/class/video4linux/v4l-subdev2/device/video4linux/video6 -o \
96
-d /sys/class/video4linux/v4l-subdev5/device/video4linux/video6 ]; then
97
PreviewDevs+=("/dev/video6")
98
PictureDevs+=("/dev/video5")
99
CameraTypes+=("mipi")
100
# use did not specify sink
101
if [ -z "${vsnk}" ]; then
102
Sinks+=(${isp_default_vsnk})
103
else
104
Sinks+=(${vsnk})
105
fi
106
PreviewModes+=("width=1280,height=720,framerate=30/1")
107
PictureModes+=("width=1920,height=1080,framerate=10/1")
108
VideoModes+=("width=1280,height=720,framerate=30/1")
109
fi
110
111
# usb camera
112
if [ -f /sys/class/video4linux/video10/name ]; then
113
if [ "$( grep -i "UVC" /sys/class/video4linux/video10/name )" ]; then
114
PreviewDevs+=("/dev/video10")
115
PictureDevs+=("/dev/video10")
116
CameraTypes+=("usb")
117
fi
118
# use did not specify sink
119
if [ -z "${vsnk}" ]; then
120
Sinks+=(${usbcamera_default_vsnk})
121
else
122
Sinks+=(${vsnk})
123
fi
124
PreviewModes+=("width=640,height=480,framerate=30/1")
125
PictureModes+=("width=640,height=480,framerate=30/1")
126
VideoModes+=("width=640,height=480,framerate=30/1")
127
fi
128
129
rkargs="device=${PreviewDevs[$icam]}"
130
rkargs_mainpath="device=${PictureDevs[$icam]}"
131
132
#----------------------------------------------------------
133
killall gst-launch-1.0 2>&1 > /dev/null
134
sleep 1
135
136
function start_preview() {
137
local CMD=
138
if [ ${CameraTypes[$icam]} = "mipi" ]; then
139
CMD="gst-launch-1.0 rkisp ${rkargs} io-mode=1 \
140
! video/x-raw,format=NV12,${PreviewModes[$icam]} \
141
! videoconvert ! ${Sinks[$icam]}"
142
else
143
CMD="gst-launch-1.0 v4l2src ${rkargs} io-mode=4 \
144
! videoconvert ! video/x-raw,format=NV12,${PreviewModes[$icam]} \
145
! ${Sinks[$icam]}"
146
fi
147
148
if [ "x${verbose}" == "xyes" ]; then
149
echo "===================================================="
150
echo "=== GStreamer 1.1 command:"
151
echo "=== $(echo $CMD | sed -e 's/\r//g')"
152
echo "===================================================="
153
fi
154
if [ ${Sinks[$icam]} = "kmssink" -o "$(id -un)" = "orangepi" ]; then
155
eval "${CMD}"&
156
else
157
su orangepi -c "${CMD}"&
158
fi
159
sleep 2
160
}
161
162
function take_photo() {
163
local CMD=
164
if [ ${CameraTypes[$icam]} = "mipi" ]; then
165
CMD="gst-launch-1.0 rkisp num-buffers=20 ${rkargs_mainpath} io-mode=1 \
166
! video/x-raw,format=NV12,${PictureModes[$icam]} \
167
! jpegenc ! multifilesink location=\"/tmp/isp-frame%d.jpg\""
168
else
169
# usb camera only support io-mode=4
170
CMD="gst-launch-1.0 v4l2src num-buffers=1 ${rkargs_mainpath} io-mode=4 \
171
! videoconvert ! video/x-raw,format=NV12,${PictureModes[$icam]} \
172
! jpegenc ! filesink location=\"/tmp/usb-frame.jpg\""
173
fi
174
175
if [ "x${verbose}" == "xyes" ]; then
176
echo "===================================================="
177
echo "=== GStreamer 1.1 command:"
178
echo "=== $(echo $CMD | sed -e 's/\r//g')"
179
echo "===================================================="
180
fi
181
echo "{{{{{{ start take photo"
182
if [ ${Sinks[$icam]} = "kmssink" -o "$(id -un)" = "orangepi" ]; then
183
eval "${CMD}"
184
else
185
su orangepi -c "${CMD}"
186
fi
187
echo "}}}}}} end take photo"
188
if [ ${CameraTypes[$icam]} = "mipi" ]; then
189
if [ -f /tmp/isp-frame19.jpg ]; then
190
cp /tmp/isp-frame19.jpg ${output}
191
fi
192
else
193
if [ -f /tmp/usb-frame.jpg ]; then
194
cp /tmp/usb-frame.jpg ${output}
195
fi
196
fi
197
}
198
199
function start_video_recording() {
200
local CMD=
201
202
if [ ${CameraTypes[$icam]} = "mipi" ]; then
203
CMD="gst-launch-1.0 rkisp num-buffers=512 ${rkargs_mainpath} io-mode=1 \
204
! video/x-raw,format=NV12,${VideoModes[$icam]} \
205
! mpph264enc ! queue ! h264parse ! mpegtsmux \
206
! filesink location=/tmp/camera-record.ts"
207
else
208
# usb camera only support io-mode=4
209
CMD="gst-launch-1.0 v4l2src num-buffers=512 ${rkargs_mainpath} io-mode=4 \
210
! videoconvert ! video/x-raw,format=NV12,${VideoModes[$icam]} \
211
! mpph264enc ! queue ! h264parse ! mpegtsmux \
212
! filesink location=/tmp/camera-record.ts"
213
fi
214
215
if [ "x${verbose}" == "xyes" ]; then
216
echo "===================================================="
217
echo "=== GStreamer 1.1 command:"
218
echo "=== $(echo $CMD | sed -e 's/\r//g')"
219
echo "===================================================="
220
fi
221
echo "{{{{{{ start video recording"
222
if [ ${Sinks[$icam]} = "kmssink" -o "$(id -un)" = "orangepi" ]; then
223
eval "${CMD}"
224
else
225
su orangepi -c "${CMD}"
226
fi
227
if [ -f /tmp/camera-record.ts ]; then
228
cp /tmp/camera-record.ts ${output}
229
fi
230
echo "}}}}}} end video recording"
231
}
232
233
234
if [ "x$action" == "xphoto" ]; then
235
take_photo
236
elif [ "x$action" == "xvideo" ]; then
237
start_video_recording
238
else
239
start_preview
240
fi
241
242