File size: 2,723 Bytes
bc0621e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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( )