Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save timothyclemansinsea/b7175f69d13d750eba27 to your computer and use it in GitHub Desktop.
Save timothyclemansinsea/b7175f69d13d750eba27 to your computer and use it in GitHub Desktop.
import cv2
import sys
import os
from datetime import datetime
startTime = datetime.now()
plusminus = 60
#os.system('rm frames/*')
#os.system('ffmpeg -threads 0 -i ihop.mp4 -f image2 frames/%05d.png')
number_of_frames = len(os.listdir('frames'))
print number_of_frames
l = [[] for i in range(number_of_frames)]
j = 0
for i in range(1, number_of_frames+1):
print j
print i
iteml = []
filename = '%05d.png' % (i)
print filename
current = os.getcwd()
# Get user supplied values
imagePath = os.path.join(current, 'frames/'+filename)
# Read the image
image = cv2.imread(imagePath)
result_image = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
for cascPath in [f for f in os.listdir('/home/azureuser/haartrainer/haarcascades') if "HS" in f or 'face' in f]:
print cascPath
cascPath = 'haarcascades/'+cascPath
faceCascade = cv2.CascadeClassifier(cascPath)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(15, 15),
)
print "Found {0} faces!".format(len(faces))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
iteml.append((x, y, w, h))
#cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
#sub_face = image[y:y+h, x:x+w]
# apply a gaussian blur on this new recangle image
#sub_face = cv2.GaussianBlur(sub_face,(23, 23), 30)
# merge this blurry rectangle to our final image
#result_image[y:y+sub_face.shape[0], x:x+sub_face.shape[1]] = sub_face
l[j].extend(iteml)
if j < plusminus:
for k in range(0,j):
l[k].extend(iteml)
else:
for k in range(j-plusminus, j):
l[k].extend(iteml)
if j + plusminus < number_of_frames:
for k in range(j+1, j+plusminus):
l[k].extend(iteml)
else:
for k in range(j+1, number_of_frames):
l[k].extend(iteml)
#cv2.imwrite("detected.png", result_image)
j += 1
j = 0
for i in range(1, number_of_frames+1):
print j
print i
iteml = []
filename = '%05d.png' % (i)
current = os.getcwd()
# Get user supplied values
imagePath = os.path.join(current, 'frames/'+filename)
# Read the image
image = cv2.imread(imagePath)
result_image = image.copy()
# Draw a rectangle around the faces
for (x, y, w, h) in l[j]:
#iteml.append((x, y, w, h))
#cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
sub_face = image[y:y+h, x:x+w]
# apply a gaussian blur on this new recangle image
sub_face = cv2.GaussianBlur(sub_face,(23, 23), 30)
# merge this blurry rectangle to our final image
result_image[y:y+sub_face.shape[0], x:x+sub_face.shape[1]] = sub_face
cv2.imwrite("output/detected_"+filename, result_image)
j += 1
os.system('ffmpeg -threads 0 -framerate 30/1 -i output/detected_%05d.png -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4')
print datetime.now() - startTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment