ソースを参照

This produced the excellent video for Greg.

Fred Damstra 6 年 前
コミット
348063d9df
1 ファイル変更48 行追加20 行削除
  1. 48 20
      facetracker.py

+ 48 - 20
facetracker.py

@@ -64,7 +64,7 @@ D=np.array([[0.0028045742945672475], [-0.14423839478882694], [0.2371510507279964
 def search_rightprofile(i):
 #    return profilefaceCascade.detectMultiScale(i,1.3,4,(cv2.CV_HAAR_DO_CANNY_PRUNING + cv2.CV_HAAR_FIND_BIGGEST_OBJECT + cv2.CV_HAAR_DO_ROUGH_SEARCH),(30,30))
     if scanright:
-        return profilefaceCascade.detectMultiScale(i, maxSize=(800,800))
+        return profilefaceCascade.detectMultiScale(i, maxSize=(30,30))
     else:
         return ()
 
@@ -73,14 +73,14 @@ def search_leftprofile(i):
     if scanleft:
         revimage = cv2.flip(i, 1) # Flip the image
         #    return profilefaceCascade.detectMultiScale(i,1.3,4,(cv2.CV_HAAR_DO_CANNY_PRUNING + cv2.CV_HAAR_FIND_BIGGEST_OBJECT + cv2.CV_HAAR_DO_ROUGH_SEARCH),(30,30))
-        return profilefaceCascade.detectMultiScale(i, maxSize=(800,800))
+        return profilefaceCascade.detectMultiScale(i, maxSize=(30,30))
     else:
         return ()
 
 
 def search_frontface(i):
 #    return frontalfaceCascade.detectMultiScale(i,1.3,4,(cv2.CV_HAAR_DO_CANNY_PRUNING + cv2.CV_HAAR_FIND_BIGGEST_OBJECT + cv2.CV_HAAR_DO_ROUGH_SEARCH),(30,30))
-    return frontalfaceCascade.detectMultiScale(i, maxSize=(800,800))
+    return frontalfaceCascade.detectMultiScale(i, maxSize=(30,30))
 
 
 def undistort(i, balance=0.0, dim2=None, dim3=None):
@@ -163,7 +163,7 @@ def findface(image):
 def circlefaces(image, faces):
     global lastface
     for (x, y, w, h) in faces:
-        cv2.circle(image, (int(x+w/2), int(y+h/2)), int((w+h)/3), (0, 255, 0), 1)
+        cv2.circle(image, (int(x+w/2), int(y+h/2)), int((w+h)/3), (255, 255, 0), 1)
         # Temporary, save the image
         cv2.imwrite("tmp/img.{}.facetype{}.png".format(datetime.now().strftime("%Y%m%d.%H%M%S.%f"), lastface), image)
 
@@ -173,6 +173,7 @@ def distance_to_closest(faces):
     closestdistance = None
     for f in faces:
          x,y,w,h = f
+         print("Face found at {},{} with width {} and height {}.".format(x,y,w,h))
          centerpoint = (w/2)+x
          distance = centerpoint - cameracenter[0]
          if(closestdistance == None or abs(distance) < closestdistance):
@@ -180,20 +181,41 @@ def distance_to_closest(faces):
              closestdistance = distance
     return closestdistance
 
-def stop():
-    print("Would stop.")
-    return
+def send_char(tc):
+    try:
+        bus.write_byte(SLAVE_ADDRESS, ord(tc))
+    except Exception as e:
+        print("Bus Error while sending {}: {}".format(tc, str(e)))
 
-def left():
-    print('Would go left.')
+def stop():
+    print("STOPPING")
+    send_char(' ')
     return
 
-def right():
-    print('Would go left.')
-    return
+def left(distance):
+    send_char('a')
+    if abs(distance) > 100:
+        print('GO LEFT')
+        return
+    else:
+        print('GO LEFT FOR {}s'.format(1.0*abs(distance)/100.0))
+        time.sleep(1.0*abs(distance)/100.0)
+        stop()
+
+def right(distance=None):
+    send_char('d')
+    if distance == None:
+        print("GO RIGHT")
+        return
+    else:
+        print('GO RIGHT FOR {}s'.format(1.0*abs(distance)/100.0))
+        time.sleep(1.0*abs(distance)/100.0)
+        stop()
 
 def fire():
-    print("Would fire.")
+    print("FIRING!")
+    send_char('F')
+    return
 
 
 if __name__ == "__main__":
@@ -210,7 +232,7 @@ if __name__ == "__main__":
         lastTime = time.time()*1000.0
 
         image = frame.array
-        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to greyscale
+#        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to greyscale
         if correct_fisheye:
             image = undistort(image, 0.8)
         faces = findface(image)
@@ -224,19 +246,25 @@ if __name__ == "__main__":
         circlefaces(image, faces)
         distance = distance_to_closest(faces)
 
-        if abs(distance) < 10 and lastAction != "Fire":
-            if lastAction == "Stop":
+        if abs(distance) < 15:
+            if lastAction == "Fire":
+                # Do nothing
+                time.sleep(1)
+            elif lastAction == "Stop":
                 lastAction = "Fire"
                 fire()
+                print("Sleeping for 15 seconds...")
+                time.sleep(15)
+                print("Resuming...")
             else:
                 lastAction = "Stop"
                 stop()
-        elif distance < 0 and lastAction != "Left":
+        elif distance < 0:
             lastAction = "Left"
-            left()
-        elif distance > 0 and lastAction != "Right":
+            left(distance)
+        elif distance > 0:
             lastAction = "Right"
-            right()
+            right(distance)
         else:
             print("Face found but no action taken. Distance = {}; Last Action = {}".format(distance, lastAction))