Last active
March 9, 2018 19:45
-
-
Save Tehnix/ffc4d63e3cd1e86b2d84 to your computer and use it in GitHub Desktop.
Streamer and receiver. The streamer broadcasts your webcam video via multicast, and the receiver displays the stream.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import socket | |
import struct | |
import cv2 | |
import numpy | |
MCAST_GRP = '224.0.0.1' | |
MCAST_PORT = 5007 | |
# Set up our multicast socket. | |
print 'Setting up connection...' | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
sock.bind((MCAST_GRP, MCAST_PORT)) | |
mreq = struct.pack('4sl', socket.inet_aton(MCAST_GRP), socket.INADDR_ANY) | |
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) | |
print 'Beginning to listen for data...' | |
cv2.namedWindow('Video Preview') | |
full_frame_length = 2764800 | |
frame_length = 1024 | |
frames_received = 0 | |
received_frames = 0 | |
data = '' | |
show_first_image = True | |
try: | |
while True: | |
# Collect data until we have a full frame. | |
data += sock.recv(frame_length) | |
frames_received += frame_length | |
if frames_received >= full_frame_length: | |
# Convert the data from a string, to a numpy matrix. | |
frame = numpy.fromstring(data, dtype=numpy.uint8) | |
frame = numpy.reshape(frame, (720,1280,3)) | |
# Display the frame. | |
cv2.imshow('Video Preview', frame) | |
cv2.waitKey(60) | |
# Reset the values after we've received the whole frame. | |
frames_received = 0 | |
data = '' | |
received_frames += 1 | |
except KeyboardInterrupt: | |
# Cleanup by destroying the window, and closing the socket. | |
cv2.destroyAllWindows() | |
sock.close() | |
print 'Received frames: ', received_frames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import socket | |
import cv2 | |
import time | |
MCAST_GRP = '224.0.0.1' | |
MCAST_PORT = 5007 | |
print 'Setting up socket...' | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32) | |
print 'Connecting to webcam...' | |
vc = cv2.VideoCapture(0) | |
vc.open(0) | |
print 'Starting data transmission...' | |
frames_sent = 0 | |
full_frame_length = 2764800 | |
frame_length = 1024 | |
# Create a window for the video test. | |
#cv2.namedWindow('Test Video Preview') | |
try: | |
while True: | |
# Capture a frame from the webcam. | |
retval, frame = vc.read() | |
# Display our test video input. | |
#cv2.imshow('Video Preview', frame) | |
# Convert the frame to a string. | |
frame = frame.flatten() | |
data = frame.tostring() | |
if retval: | |
# We divide the fram into chunks, else it will exceed the maximum | |
# block length. | |
length_of_data = len(data) | |
eaten_data = 0 | |
while (eaten_data + frame_length) <= length_of_data: | |
data_chunk = data[eaten_data:eaten_data+frame_length] | |
eaten_data += frame_length | |
sock.sendto(data_chunk, (MCAST_GRP, MCAST_PORT)) | |
frames_sent += 1 | |
except KeyboardInterrupt: | |
vc.release() | |
sock.close() | |
cv2.destroyAllWindows() | |
print 'Frames sent: ', frames_sent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment