parent
f7703b8dad
commit
9c5beaa5a0
File diff suppressed because one or more lines are too long
@ -0,0 +1,93 @@
|
|||||||
|
import cv2
|
||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import matplotlib.animation as animation
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
class VideoCaptureTransfer:
|
||||||
|
def __init__(self, source=0, dest_ip='127.0.0.1', dest_port=8000):
|
||||||
|
self.capture = cv2.VideoCapture(source)
|
||||||
|
|
||||||
|
self.dest_ip = dest_ip
|
||||||
|
self.dest_port = dest_port
|
||||||
|
|
||||||
|
self.frame = None
|
||||||
|
self.save_video = False
|
||||||
|
|
||||||
|
self.lock = threading.Lock()
|
||||||
|
|
||||||
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
|
||||||
|
self.thread = threading.Thread(target=self._capture_send)
|
||||||
|
self.thread.start()
|
||||||
|
|
||||||
|
def _capture_send(self):
|
||||||
|
while self.capture.isOpened():
|
||||||
|
ret, frame = self.capture.read()
|
||||||
|
if not ret:
|
||||||
|
break
|
||||||
|
|
||||||
|
frame = cv2.resize(frame, (640, 480))
|
||||||
|
|
||||||
|
if self.save_video:
|
||||||
|
self.output.write(frame)
|
||||||
|
|
||||||
|
with self.lock:
|
||||||
|
self.frame = frame.copy()
|
||||||
|
|
||||||
|
_, data = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
|
||||||
|
|
||||||
|
self.sock.sendto(data.tobytes(), (self.dest_ip, self.dest_port))
|
||||||
|
|
||||||
|
self.capture.release()
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
self.output.release()
|
||||||
|
|
||||||
|
def start_recording(self, filename="output.avi", fmt="XVID"):
|
||||||
|
fourcc = cv2.VideoWriter_fourcc(*fmt)
|
||||||
|
fps = int(self.capture.get(cv2.CAP_PROP_FPS))
|
||||||
|
frame_size = (int(self.capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
|
||||||
|
self.output = cv2.VideoWriter(filename, fourcc, fps, frame_size)
|
||||||
|
self.save_video = True
|
||||||
|
|
||||||
|
def stop_recording(self):
|
||||||
|
self.save_video = False
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
with self.lock:
|
||||||
|
return self.frame
|
||||||
|
|
||||||
|
def release(self):
|
||||||
|
self.thread.join()
|
||||||
|
self.sock.close()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
vct = VideoCaptureTransfer(0,"192.168.43.18",9999)
|
||||||
|
|
||||||
|
def on_key_press(event):
|
||||||
|
if event.key == ' ':
|
||||||
|
if not vct.save_video:
|
||||||
|
vct.start_recording()
|
||||||
|
|
||||||
|
def on_key_release(event):
|
||||||
|
if event.key == ' ':
|
||||||
|
if vct.save_video:
|
||||||
|
vct.stop_recording()
|
||||||
|
|
||||||
|
fig, ax = plt.subplots()
|
||||||
|
im = ax.imshow(np.zeros((480, 640, 3), dtype=np.uint8))
|
||||||
|
|
||||||
|
def update_frame(_):
|
||||||
|
frame = vct.read()
|
||||||
|
im.set_data(frame)
|
||||||
|
return [im]
|
||||||
|
|
||||||
|
ani = animation.FuncAnimation(fig, update_frame, interval=50, blit=True)
|
||||||
|
|
||||||
|
fig.canvas.mpl_connect('key_press_event', on_key_press)
|
||||||
|
fig.canvas.mpl_connect('key_release_event', on_key_release)
|
||||||
|
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
vct.release()
|
Loading…
Reference in new issue