Spaces:
Runtime error
Runtime error
# import cv2 | |
# import socket | |
# import io | |
# import struct | |
# # Define connection details (replace with your actual values) | |
# HOST = '144.126.254.143' # IP address of the receiving device | |
# PORT = 5555 # Port number for communication | |
# # Create a socket connection | |
# connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# connection.connect((HOST, PORT)) | |
# print("Starting camera using OpenCV...") | |
# # Initialize video capture using OpenCV | |
# cap = cv2.VideoCapture(0) # Change 0 to the video source index if needed | |
# while True: | |
# # Capture frame-by-frame | |
# ret, frame = cap.read() | |
# # Check if frame capture is successful | |
# if not ret: | |
# print("Error: Frame not captured") | |
# break | |
# # Encode frame as JPEG | |
# _, jpeg_buffer = cv2.imencode('.jpg', frame) | |
# # Create a stream-like object from the JPEG buffer | |
# stream = io.BytesIO(jpeg_buffer.tobytes()) | |
# # Get the stream's current position (similar to stream.tell()) | |
# stream_length = stream.tell() | |
# # Send frame size (4 bytes) in little-endian format | |
# connection.sendall(struct.pack('<L', stream_length)) | |
# # Reset the stream position (similar to stream.seek(0)) | |
# stream.seek(0) | |
# # Send the entire JPEG frame | |
# connection.sendall(stream.read()) | |
# # Reset the stream for the next frame | |
# stream.seek(0) | |
# stream.truncate() | |
# # Display the frame for preview (optional) | |
# cv2.imshow('Frame', frame) | |
# # Exit on 'q' key press | |
# if cv2.waitKey(1) == ord('q'): | |
# break | |
# # Release resources | |
# cap.release() | |
# connection.close() | |
# cv2.destroyAllWindows() | |
# print("Camera and connection closed") | |
import struct | |
import time | |
import socket | |
import cv2 | |
SERVER_PORT=5555 | |
SERVER_IP='192.168.137.1' | |
# Define the codec using VideoWriter_fourcc and create a VideoWriter object | |
fourcc = cv2.VideoWriter_fourcc(*'H264') | |
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640, 480)) | |
cap=cv2.VideoCapture(0) | |
client_socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
client_socket.connect((SERVER_IP,SERVER_PORT)) | |
count=0 | |
while True: | |
time.sleep(0.05) | |
count += 1 | |
print('sending frame', count) | |
ret, frame = cap.read() | |
if ret: | |
# Write the frame into the file 'output.mp4' | |
out.write(frame) | |
# Convert the frame to bytes | |
frame_encoded = cv2.imencode('.jpg', frame)[1].tostring() | |
# Send the size of the frame | |
client_socket.sendall(len(frame_encoded).to_bytes(4, 'big')) | |
# Send the frame | |
client_socket.sendall(frame_encoded) | |
else: | |
break | |
if cv2.waitKey(1)==ord('q'): | |
break | |
cap.release() | |
client_socket.close() | |
cv2.destroyAllWindows( ) |