From 20d02045b55b21c589cdac5d8e47eaf7b625a5de Mon Sep 17 00:00:00 2001
From: Ouhouuhu <40174296+Ouhouuhu@users.noreply.github.com>
Date: Tue, 28 Feb 2023 15:10:23 +0100
Subject: [PATCH] possibilite de conduire la voiture pour avoir commande fiable

---
 .../yolov3/car_interface.py                   |  2 +-
 .../yolov3/pilote.py                          | 14 +++++++++++
 .../yolov3/utils.py                           | 23 +++++++++++--------
 3 files changed, 28 insertions(+), 11 deletions(-)
 create mode 100644 PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/pilote.py

diff --git a/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/car_interface.py b/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/car_interface.py
index 7b099a6..d11cbdc 100644
--- a/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/car_interface.py	
+++ b/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/car_interface.py	
@@ -14,7 +14,7 @@ def close_ser(ser):
     ser.close()
 
 def send_infos(ser, a,b,c):
-    n = str(a)+' '+str(b)+' '+str(c)
+    n = f"['{a} {b} {c}']"
     #print("input: "+str(n))
     ser.write((str(n)+'\n').encode("utf-8"))
     cc=str(ser.readline())
diff --git a/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/pilote.py b/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/pilote.py
new file mode 100644
index 0000000..47723ca
--- /dev/null
+++ b/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/pilote.py	
@@ -0,0 +1,14 @@
+import keyboard
+
+def get_command():
+    forward = 0
+    direction = 30
+    if keyboard.is_pressed('q'):
+        direction=-50
+    elif keyboard.is_pressed('d'):
+        direction=110
+    if keyboard.is_pressed('z'):
+        forward = 10
+    if keyboard.is_pressed('s'):
+        forward = -10
+    return forward, direction, 0
\ No newline at end of file
diff --git a/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/utils.py b/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/utils.py
index 997a53f..a5949a3 100644
--- a/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/utils.py	
+++ b/PAR 152/Yolo V3/TensorFlow-2.x-YOLOv3-master/yolov3/utils.py	
@@ -27,7 +27,7 @@ from shapely.geometry import Polygon, Point, LineString
 from yolov3.car_interface import get_ser, start_ser, close_ser, send_infos
 
 from MCL import get_position
-
+import yolov3.pilote as pilot
 
 
 class liste_pos:
@@ -221,24 +221,26 @@ def draw_bbox(image, bboxes,liste_pos_car, CLASSES=YOLO_COCO_CLASSES, show_label
     plt.plot([i for i,j in track_points], [j for i,j in track_points])
     plt.show()
 
+    speed, commande, a = pilot.get_command()
+    
     """calculate distance"""
-    front_center = Point(np.array([x_car,y_car]))
-    error = track.distance(front_center)
+    #front_center = Point(np.array([x_car,y_car]))
+    #error = track.distance(front_center)
     
     """calculate PID response"""
-    pid = PidController(50,0.005,5) #P, I, D
-    commande = pid.get_control(error)
-    commande -= 30
+    #pid = PidController(50,0.005,5) #P, I, D
+    #commande = pid.get_control(error)
+    #commande -= 30
     
     print(f"command: {commande}")
     
     """sending info to car"""
     try:
-        print(f"poten: {send_infos(ser,10,commande,0)}")
+        print(f"poten: {send_infos(ser,speed,commande,0)}")
     except:
         pass
     
-    return image, commande, liste_pos_car
+    return image, speed, commande, liste_pos_car
 
 
 def bboxes_iou(boxes1, boxes2):
@@ -584,11 +586,12 @@ def detect_realtime(Yolo, output_path, input_size=416, show=False, CLASSES=YOLO_
         codec = cv2.VideoWriter_fourcc(*'XVID')
         out = cv2.VideoWriter(output_path, codec, fps, (width, height)) # output_path must be .mp4
 
-    liste_pos_car = liste_pos((278/225, 200/225,3.1415/2), 50)
+    liste_pos_car = liste_pos((1, 0.5,3.1415/2), 50)
 
     ser = get_ser()
     start_ser(ser)
     command=0
+    speed=0
     
     while True:
         ret, frame = vid.read()
@@ -633,7 +636,7 @@ def detect_realtime(Yolo, output_path, input_size=416, show=False, CLASSES=YOLO_
         
         print("Time: {:.2f}ms, {:.1f} FPS".format(ms, fps))
 
-        frame, command, liste_pos_car = draw_bbox(original_frame, bboxes,liste_pos_car, CLASSES=CLASSES, rectangle_colors=rectangle_colors, ser=ser, previous_command=(10, command, fps))
+        frame, speed, command, liste_pos_car = draw_bbox(original_frame, bboxes,liste_pos_car, CLASSES=CLASSES, rectangle_colors=rectangle_colors, ser=ser, previous_command=(speed, command, fps))
         # CreateXMLfile("XML_Detections", str(int(time.time())), original_frame, bboxes, read_class_names(CLASSES))
         image = cv2.putText(frame, "Time: {:.1f}FPS".format(fps), (0, 30),
                           cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2)
-- 
GitLab