|
@@ -8,6 +8,7 @@ import sys
|
|
|
import imutils
|
|
|
import np
|
|
|
import math # for sqrt distance formula
|
|
|
+import random
|
|
|
|
|
|
# i2c stuff
|
|
|
import smbus
|
|
@@ -167,12 +168,16 @@ def findface(image):
|
|
|
return faces
|
|
|
return ()
|
|
|
|
|
|
+
|
|
|
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), (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)
|
|
|
+ if random.randint(1, 10) == 1:
|
|
|
+ # Save 1 out of 10
|
|
|
+ cv2.imwrite("tmp/img.{}.facetype{}.png".format(datetime.now().strftime("%Y%m%d.%H%M%S.%f"), lastface), image)
|
|
|
+
|
|
|
|
|
|
def distance_to_closest(faces):
|
|
|
# Negative values will be left
|
|
@@ -237,6 +242,7 @@ if __name__ == "__main__":
|
|
|
lastTime = time.time()*1000.0
|
|
|
lastDetected = lastTime
|
|
|
lastAction = None
|
|
|
+ totalFound = 0 # We don't want to fire too soon.
|
|
|
|
|
|
# capture frames from the camera
|
|
|
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
|
|
@@ -250,6 +256,16 @@ if __name__ == "__main__":
|
|
|
# image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to greyscale
|
|
|
if correct_fisheye:
|
|
|
image = undistort(image, 0.8)
|
|
|
+ if lastAction == "Fire":
|
|
|
+ # Save the pic, sleep, and then resume
|
|
|
+ lastAction = "Stop"
|
|
|
+ cv2.imwrite("tmp/img.{}.FIRED.png".format(datetime.now().strftime("%Y%m%d.%H%M%S.%f")), image)
|
|
|
+ print("Sleeping for 15 seconds...")
|
|
|
+ time.sleep(15)
|
|
|
+ print("Resuming...")
|
|
|
+ rawCapture.truncate(0)
|
|
|
+ continue
|
|
|
+
|
|
|
faces = findface(image)
|
|
|
if faces == ():
|
|
|
print("No face found.")
|
|
@@ -270,6 +286,7 @@ if __name__ == "__main__":
|
|
|
stop()
|
|
|
continue
|
|
|
|
|
|
+ totalFound += 1
|
|
|
lastDetected = time.time() * 1000.0
|
|
|
circlefaces(image, faces)
|
|
|
distance = distance_to_closest(faces)
|
|
@@ -283,12 +300,11 @@ if __name__ == "__main__":
|
|
|
if lastAction == "Fire":
|
|
|
# Do nothing
|
|
|
time.sleep(1)
|
|
|
- elif lastAction == "Stop":
|
|
|
+ elif lastAction == "Stop" and totalFound >= 5:
|
|
|
+ totalFound = 0 # Restart the counter
|
|
|
lastAction = "Fire"
|
|
|
+ cv2.imwrite("tmp/img.{}.ABOUTTOFIRE.png".format(datetime.now().strftime("%Y%m%d.%H%M%S.%f")), image)
|
|
|
fire()
|
|
|
- print("Sleeping for 15 seconds...")
|
|
|
- time.sleep(15)
|
|
|
- print("Resuming...")
|
|
|
else:
|
|
|
lastAction = "Stop"
|
|
|
stop()
|