Kaynağa Gözat

Initial combination of facetracker and controls.

Fred Damstra 6 yıl önce
ebeveyn
işleme
0843175e7c
1 değiştirilmiş dosya ile 41 ekleme ve 1 silme
  1. 41 1
      facetracker.py

+ 41 - 1
facetracker.py

@@ -9,6 +9,14 @@ import imutils
 import np
 import math # for sqrt distance formula
 
+# i2c stuff
+import smbus
+
+bus = smbus.SMBus(1)
+SLAVE_ADDRESS = 0x04
+everFound = False # have we ever found a face?
+lastmovement = None # set to 'right' or left'
+
 # Create the haar cascade
 frontalfaceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
 profilefaceCascade = cv2.CascadeClassifier("haarcascade_profileface.xml")
@@ -161,6 +169,7 @@ for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=
 
 
     if faceFound:
+        everFound = True
         print("{}: {} faces found. Type: {}".format(time.time()*1000.0-lastTime, len(faces), lastface))
 
         # Draw a rectangle around the faces
@@ -190,7 +199,38 @@ for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=
     # Determine directions and distance
     travel = [ (cameracenter[0] - target[0]) / camera.resolution[0], (cameracenter[1] - target[1]) /camera.resolution[1] ]
     print("To move horizontal: {}, vertical: {}".format(travel[0], travel[1]))
-    
 
+    # horizontal movement
+    if abs(travel[0]) < 10.0 or everFound == False:
+        # Fire!
+        everFound = False # No face found since last detection
+        try:
+            bus.write_byte(SLAVE_ADDRESS, ord('F'))
+            print("Sent '{}' to arduino.".format(ord(var('F'))))
+        except:
+            print("Bus I/O error. Continuing.")
+            continue
+
+    if travel[0] > 0 and lastmovement != "right":
+        # Move right
+        lastmovement = "right"
+        try:
+            bus.write_byte(SLAVE_ADDRESS, ord(' '))
+            bus.write_byte(SLAVE_ADDRESS, ord('d'))
+            print("Sent '{}' to arduino.".format(ord(var('d'))))
+        except:
+            print("Bus I/O error. Continuing.")
+            continue
+
+    if travel[0] < 0 and lastmovement != "left":
+        # Move left
+        lastmovement = "left"
+        try:
+            bus.write_byte(SLAVE_ADDRESS, ord(' '))
+            bus.write_byte(SLAVE_ADDRESS, ord('a'))
+            print("Sent '{}' to arduino.".format(ord(var('a'))))
+        except:
+            print("Bus I/O error. Continuing.")
+            continue