-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive_server.py
66 lines (54 loc) · 1.76 KB
/
drive_server.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
import socket
import cv2
from picarx import Picarx
import struct
import numpy as np
from time import sleep, time
# Initialize PiCar-X
px = Picarx()
px.forward(0)
# Set up the webcam
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# TCP Server setup
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host_ip = '0.0.0.0' # Listen on all network interfaces
port = 12345
server_socket.bind((host_ip, port))
server_socket.listen(1)
print("Waiting for a connection.")
connection, addr = server_socket.accept()
print("Connected to", addr)
def send_frame(image):
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
result, frame = cv2.imencode('.jpg', image, encode_param)
data = frame.tobytes()
connection.sendall(struct.pack("<L", len(data)) + data)
try:
while True:
# Capture frame from webcam
ret, frame = cap.read()
if not ret:
print(".")
continue
# Resize frame to 224x224
frame = cv2.resize(frame, (224, 224))
# Send frame to client
send_frame(frame)
# Receive steering and forward_value
received_data = connection.recv(8) # 2 floats, 4 bytes each
if not received_data:
break
steering, forward_value = struct.unpack('ff', received_data)
# print("Received - Steering:", steering, "Forward value:", forward_value)
# Set steering and forward on PiCar-X
px.set_dir_servo_angle((steering * 20) + (0.3 * 20))
forward_value = 2
px.forward(forward_value)
sleep(0.05)
finally:
cap.release()
connection.close()
server_socket.close()