thot-detector/thot_detector.py
2022-08-01 19:39:28 -05:00

67 lines
1.8 KiB
Python

#code modified from https://pythonprogramming.net/haar-cascade-object-detection-python-opencv-tutorial/
import numpy as np
import cv2
import os
from pygame import mixer
mixer.init()
mixer.music.load("begone_thot.mp3")
thot_cascade = cv2.CascadeClassifier('face_cascade.xml')
cap = cv2.VideoCapture(0)
launched = False
capturing = False
saved = 0
recent_saved = 0
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# add this
# image, reject levels level weights.
thots = thot_cascade.detectMultiScale(gray, 1.3, 5)
# add this
for (x,y,w,h) in thots:
#speech.say("Thought Detected")
#speech.runAndWait()
font = cv2.FONT_HERSHEY_SIMPLEX
if not launched:
pass
#os.system("ard.exe")
capturing = True
recent_saved = 0
if (not mixer.music.get_busy()):
mixer.music.play()
cv2.putText(img, 'THOT',(x+w,y+h), font, 0.5, (11,255,255), 2, cv2.LINE_AA)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
if capturing and recent_saved < 60:
cv2.imwrite("M:/Pictures/saved_thots/detected_thot_" + str(saved) + ".jpg", img)
saved += 1
recent_saved += 1
"""
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
"""
cv2.imshow('Thot Detector',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()