Skip to content

Commit

Permalink
Add haarscade support for mp4
Browse files Browse the repository at this point in the history
  • Loading branch information
iitzco committed Aug 22, 2018
1 parent 24a501b commit 9705c96
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
6 changes: 4 additions & 2 deletions const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
TRAIN_TF_RECORD_FILE = os.path.join(INPUT_DIR, "train.tfrecord")
VAL_TF_RECORD_FILE = os.path.join(INPUT_DIR, "val.tfrecord")

YOLO_SIZE = 352
YOLO_TARGET = 11
# YOLO_SIZE = 352
# YOLO_TARGET = 11
YOLO_SIZE = 288
YOLO_TARGET = 9

CORRECTOR_SIZE = 50

Expand Down
2 changes: 1 addition & 1 deletion detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def load_model_pb(self, model_dir):
self.sess = tf.Session()


def predict(self, frame, thresh=0.8):
def predict(self, frame, thresh=0.9):
input_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
input_img = cv2.resize(input_img, (YOLO_SIZE, YOLO_SIZE)) / 255.
input_img = np.reshape(input_img, [1, YOLO_SIZE, YOLO_SIZE, 3])
Expand Down
68 changes: 68 additions & 0 deletions haarcascade_video_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import cv2
import sys
import time

from detector import FaceDetector
from utils import annotate_image


def run(feed):
cascPath = "./env/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)

if feed is None:
# From webcam
cap = cv2.VideoCapture(0)
else:
cap = cv2.VideoCapture(feed)

# Get current width of frame
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float
# Get current height of frame
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float
fps = cap.get(cv2.CAP_PROP_FPS) # float


# Define the codec and create VideoWriter object
# fourcc = cv2.Video.CV_FOURCC(*'X264')
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter("output.avi",fourcc, fps, (int(width),int(height)))

now = time.time()
while(cap.isOpened()):
now = time.time()

ret, frame = cap.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

now = time.time()

faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

out.write(frame)

# Display the resulting frame
# cv2.imshow('frame', ann_frame)
print("FPS: {:0.2f}".format(1 / (time.time() - now)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


if __name__ == "__main__":
feed = None if len(sys.argv) == 1 else sys.argv[1]
run(feed)
14 changes: 8 additions & 6 deletions video_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from detector import FaceDetector
from utils import annotate_image

YOLO_MODELS_DIR = "/home/ivanitz/yolo-face/models/"
CORRECTOR_MODELS_DIR = "/home/ivanitz/face-correction/models/"
YOLO_MODELS_DIR = "../yolo-face-artifacts/run8/models/"
CORRECTOR_MODELS_DIR = "../fine-tuned-face/models/"


def run(feed):
Expand All @@ -33,20 +33,22 @@ def run(feed):

now = time.time()
while(cap.isOpened()):
now = time.time()
# Capture frame-by-frame
ret, frame = cap.read()

now = time.time()
# now = time.time()
bboxes = face_detector.predict(frame)
print("FPS: {:0.2f}".format(1 / (time.time() - now)))
# print("FPS: {:0.2f}".format(1 / (time.time() - now)))
ann_frame = annotate_image(frame, bboxes)

out.write(ann_frame)

# Display the resulting frame
# cv2.imshow('frame', ann_frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
print("FPS: {:0.2f}".format(1 / (time.time() - now)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture
cap.release()
Expand Down

0 comments on commit 9705c96

Please sign in to comment.