-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreceiver.py
239 lines (183 loc) · 9.11 KB
/
receiver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# # MIT License
# # Copyright (c) 2019 JetsonHacks
# # See LICENSE for OpenCV license and additional information
# # https://docs.opencv.org/3.3.1/d7/d8b/tutorial_py_face_detection.html
# # On the Jetson Nano, OpenCV comes preinstalled
# # Data files are in /usr/sharc/OpenCV
# import numpy as np
# import cv2
# import pyrealsense2 as rs
# from multiprocessing import Process
# # gstreamer_pipeline returns a GStreamer pipeline for capturing from the CSI camera
# # Defaults to 1280x720 @ 30fps
# # Flip the image by setting the flip_method (most common values: 0 and 2)
# # display_width and display_height determine the size of the window on the screen
# # def gstreamer_pipeline(capture_width=3280, capture_height=2464, output_width=224, output_height=224, framerate=21, flip_method=0) :
# # return 'nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM), width=%d, height=%d, format=(string)NV12, framerate=(fraction)%d/1 ! nvvidconv flip-method=%d ! nvvidconv ! video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! videoconvert ! appsink' % (
# # capture_width, capture_height, framerate, flip_method, output_width, output_height)
# fps = 30.
# frame_width = 640
# frame_height = 480
# client_ip='127.0.0.1'
# # cap = cv2.VideoCapture("v4l2src device=/dev/video=0 ! video/x-raw,framerate=30/1 ! appsink")
# cap = cv2.VideoCapture('udpsrc port=5200 caps=application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)H264,payload=(int)96!rtph264depay!decodebin!videoconvert!appsink',cv2.CAP_GSTREAMER)
# if not cap.isOpened():
# print("cannot caputure from camera. Exiting")
# quit()
# gst_str_rtp = 'appsrc ! videoconvert ! x264enc ! h264parse ! rtph264pay pt=96 ! udpsink host=127.0.0.1 port=5000'
# def send():
# cap_send =cv2.VideoWriter(gst_str_rtp, 0, fps, (frame_width, frame_height), True)
# # if not cap_send.isOpened():
# # print('VideoCapture or VideoWriter not opened')
# # exit(0)
# while True:
# ret,frame = cap_send.read()
# if not ret:
# print('empty frame')
# break
# out_send.write(frame)
# cv2.imshow('send', frame)
# if cv2.waitKey(1)&0xFF == ord('q'):
# break
# cap_send.release()
# # def sendrs():
# # out_send = cv2.VideoWriter('appsrc ! videoconvert ! video/x-raw, format=(string)BGRx, width=(int)1280, height=(int)720, framerate=(fraction)30/1 ! videoconvert ! video/x-raw, format=(string)I420 ! omxh264enc control-rate=2 bitrate=4000000 ! video/x-h264, stream-format=byte-stream ! rtph264pay mtu=1400 ! udpsink host=%s port=5001 sync=false async=false'%(client_ip),cv2.CAP_GSTREAMER,0,30,(1280,720), True)
# # pipe = rs.pipeline()
# # cfg = rs.config()
# # #cfg.enable_stream(rs.stream.gyro)
# # cfg.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30) # color camera
# # pipe.start(cfg)
# # while True:
# # frames = pipe.wait_for_frames()
# # color_frame = frames.get_color_frame()
# # img = np.asanyarray(color_frame.get_data())
# # #cv2.imshow('send1', img)
# # #if cv2.waitKey(1)&0xFF == ord('q'):
# # #break
# # out_send.write(img)
# # out_send.release()
# if __name__ == '__main__':
# #send()
# #sendrs()
# s = Process(target=send)
# # r = Process(target=sendrs)
# s.start()
# # r.start()
# s.join()
# # r.join()
# cv2.destroyAllWindows()
import time
import cv2
from multiprocessing import Process
# # Cam properties
# fps = 30.
# frame_width = 640
# frame_height = 480
# # Create capture
# cap = cv2.VideoCapture('videotestsrc ! video/x-raw,framerate=30/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
# if not cap.isOpened():
# print("Cannot open camera. Exiting.")
# quit()
# # cap = cv2.VideoCapture(0)
# # Set camera properties
# # cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
# # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)
# # cap.set(cv2.CAP_PROP_FPS, fps)
# # Define the gstreamer sink
# # gst_str_rtp = "appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000"
# gst_str_rtp = "appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000"
# # Check if cap is open
# # if cap.isOpened() is not True:
# # print("Cannot open camera. Exiting.")
# # quit()
# # Create videowriter as a SHM sink
# out = cv2.VideoWriter(gst_str_rtp, 0, fps, (frame_width, frame_height), True)
# # Loop it
# while True:
# # Get the frame
# ret, frame = cap.read()
# # Check
# if ret is True:
# # Flip frame
# frame = cv2.flip(frame, 1)
# # Write to SHM
# out.write(frame)
# print("in")
# else:
# print("Camera error.")
# time.sleep(10)
# cap.release()
def send():
cap_send = cv2.VideoCapture('v4l2src device=/dev/video0 ! video/x-raw,framerate=30/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
out_send = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency ! rtph264pay pt=96 ! udpsink host=127.0.0.1 port=5000',cv2.CAP_GSTREAMER,0, 30, (640,480), True)
if not cap_send.isOpened() or not out_send.isOpened():
print('VideoCapture or VideoWriter not opened')
exit(0)
while True:
ret,frame = cap_send.read()
if not ret:
print('empty frame')
break
out_send.write(frame)
# cv2.imshow('send', frame)
# if cv2.waitKey(1)&0xFF == ord('q'):
# break
cap_send.release()
out_send.release()
# def receive():
# # cap_receive = cv2.VideoCapture('udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
# cap_receive = cv2.VideoCapture('udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! h264parse ! avdec_h264 ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
# if not cap_receive.isOpened():
# print('VideoCapture not opened')
# exit(0)
# while True:
# ret,frame = cap_receive.read()
# if not ret:
# print('empty frame')
# break
# cv2.imshow('receive', frame)
# if cv2.waitKey(1)&0xFF == ord('q'):
# break
# #cap_receive.release()
def receive1():
# cap_receive = cv2.VideoCapture('udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
# cap_receive = cv2.VideoCapture('udpsrc port=5001 caps = "application/x-rtp, clock-rate=(int)90000, encoding-name=(string)X-GST, payload=(int)96" ! rtpgstdepay ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
# cap_receive = cv2.VideoCapture('udpsrc port="5000" caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)RAW, sampling=(string)RGB, width=(string)640, height=(string)480, payload=(int)96" ! rtpvrawdepay ! videoconvert ! queue ! appsink', cv2.CAP_GSTREAMER)
cap_receive = cv2.VideoCapture('udpsrc port=5001 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
if not cap_receive.isOpened():
print('VideoCapture not opened')
exit(0)
while True:
ret,frame = cap_receive.read()
if not ret:
print('empty frame')
break
cv2.imshow('receive1', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
def receive2():
cap_receive = cv2.VideoCapture('udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
# cap_receive = cv2.VideoCapture('udpsrc port=5001 caps = "application/x-rtp, clock-rate=(int)90000, encoding-name=(string)X-GST, payload=(int)96" ! rtpgstdepay ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
# cap_receive = cv2.VideoCapture('udpsrc port="5000" caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)RAW, sampling=(string)RGB, width=(string)640, height=(string)480, payload=(int)96" ! rtpvrawdepay ! videoconvert ! queue ! appsink', cv2.CAP_GSTREAMER)
if not cap_receive.isOpened():
print('VideoCapture not opened')
exit(0)
while True:
ret,frame = cap_receive.read()
if not ret:
print('empty frame')
break
cv2.imshow('receive2', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
if __name__ == '__main__':
# s = Process(target=send)
r1 = Process(target=receive1)
r2 = Process(target=receive2)
# s.start()
r1.start()
r2.start()
# s.join()
r1.join()
r2.join()
cv2.destroyAllWindows()