소스 검색

Misc improvements.

Fred Damstra 6 년 전
부모
커밋
5902fc93ff
1개의 변경된 파일29개의 추가작업 그리고 58개의 파일을 삭제
  1. 29 58
      facetracker.py

+ 29 - 58
facetracker.py

@@ -15,6 +15,9 @@ import smbus
 bus = smbus.SMBus(1)
 SLAVE_ADDRESS = 0x04
 
+time_before_scan = 3*60*1000.0
+time_after_scan   = (3*60+15)*1000.0
+
 # Create the haar cascade
 #frontalfaceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
 frontalfaceCascade = cv2.CascadeClassifier("frontalface_fromweb.xml")
@@ -62,17 +65,21 @@ D=np.array([[0.0028045742945672475], [-0.14423839478882694], [0.2371510507279964
 
 
 def search_rightprofile(i):
+    return ()
 #    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)
         return profilefaceCascade.detectMultiScale(i, maxSize=(30,30))
     else:
         return ()
 
 
 def search_leftprofile(i):
+    return ()
     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)
         return profilefaceCascade.detectMultiScale(i, maxSize=(30,30))
     else:
         return ()
@@ -80,6 +87,7 @@ def search_leftprofile(i):
 
 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)
     return frontalfaceCascade.detectMultiScale(i, maxSize=(30,30))
 
 
@@ -194,7 +202,7 @@ def stop():
 
 def left(distance):
     send_char('a')
-    if abs(distance) > 100:
+    if abs(distance) > 75:
         print('GO LEFT')
         return
     else:
@@ -204,7 +212,7 @@ def left(distance):
 
 def right(distance=None):
     send_char('d')
-    if distance == None:
+    if abs(distance) > 75:
         print("GO RIGHT")
         return
     else:
@@ -222,13 +230,15 @@ if __name__ == "__main__":
     # allow the camera to warmup
     time.sleep(0.1)
     lastTime = time.time()*1000.0
+    lastDetected = lastTime
     lastAction = None
 
     # capture frames from the camera
     for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
             # grab the raw NumPy array representing the image, then initialize the timestamp
             # and occupied/unoccupied text
-        print('Time: {}'.format(time.time()*1000.0 - lastTime))
+        timeSinceDetected = 1000.0*time.time() - lastDetected
+        print('Time: {}; Time since detected: {} ({}/{})'.format(time.time()*1000.0 - lastTime, timeSinceDetected, time_before_scan, time_after_scan))
         lastTime = time.time()*1000.0
 
         image = frame.array
@@ -241,12 +251,27 @@ if __name__ == "__main__":
             stop()
             lastAction = "Stop"
             rawCapture.truncate(0)
+            if timeSinceDetected > time_before_scan and timeSinceDetected < time_after_scan and lastAction != "Scan":
+                lastAction = "Scan"
+                print("Beginning Scanning...")
+                right(1000) # 1000 is arbitrary
+            elif timeSinceDetected > time_after_scan:
+                lastAction = "Stop"
+                print("Stopping scanning...")
+                lastDetected = time.time()*1000.0
+                stop()
             continue
 
+        lastDetected = time.time() * 1000.0
         circlefaces(image, faces)
         distance = distance_to_closest(faces)
 
-        if abs(distance) < 15:
+        if lastAction == "Scan":
+            # Scanning, but detected a face. Stop and continue
+            print("Face detected. Aborted scanning.")
+            stop()
+            lastAction = "Stop"
+        elif abs(distance) < 15:
             if lastAction == "Fire":
                 # Do nothing
                 time.sleep(1)
@@ -271,57 +296,3 @@ if __name__ == "__main__":
         # clear the stream in preparation for the next frame
         rawCapture.truncate(0)
 
-        # horizontal movement
-#        if abs(travel[0]) < 0.01 and everFound == True:
-#            # Fire!
-#            everFound = False # No face found since last firing
-#            try:
-#                bus.write_byte(SLAVE_ADDRESS, ord(' '))
-#                time.sleep(1)
-#                bus.write_byte(SLAVE_ADDRESS, ord('F'))
-#                bus.write_i2c_block_data(SLAVE_ADDRESS, ord(' '), [ord('F')])
-#                print("Sent '{}' to arduino.".format(ord('F')))
-#            except:
-#                print("Bus I/O error: {}".format(str(e)))
-#                continue
-#
-#        if travel[0] < 0 and lastmovement != "right":
-#            # Move right
-#            lastmovement = "right"
-#            print("Moving right.")
-#            try:
-#                bus.write_byte(SLAVE_ADDRESS, ord(' '))
-#                time.sleep(1)
-#                bus.write_byte(SLAVE_ADDRESS, ord('d'))
-#                time.sleep(1)
-#                bus.write_byte(SLAVE_ADDRESS, ord('d'))
-#                time.sleep(1)
-#                bus.write_byte(SLAVE_ADDRESS, ord('d'))
-#                time.sleep(1)
-#                bus.write_byte(SLAVE_ADDRESS, ord('d'))
-#                print("Sent '{}' to arduino.".format(ord('d')))
-#            except Exception as e:
-#                print("Bus I/O error: {}".format(str(e)))
-#                continue
-#
-#        if travel[0] > 0 and lastmovement != "left":
-#            # Move left
-#            lastmovement = "left"
-#            print("Moving left.")
-#            try:
-#                bus.write_byte(SLAVE_ADDRESS, ord(' '))
-#                time.sleep(1)
-#                bus.write_byte(SLAVE_ADDRESS, ord('a'))
-#                time.sleep(1)
-#                bus.write_byte(SLAVE_ADDRESS, ord('a'))
-#                time.sleep(1)
-#                bus.write_byte(SLAVE_ADDRESS, ord('a'))
-#                time.sleep(1)
-#                bus.write_byte(SLAVE_ADDRESS, ord('a'))
-#                time.sleep(1)
-#                print("Sent '{}' to arduino.".format(ord('a')))
-#            except:
-#                print("Bus I/O error: {}".format(str(e)))
-#                continue
-
-