-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandrew_scratchwork.py
More file actions
40 lines (26 loc) · 1.08 KB
/
andrew_scratchwork.py
File metadata and controls
40 lines (26 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import cv2
import numpy as np
import os
# make output dir
if not os.path.exists("output"):
os.makedirs("output")
cascade = cv2.CascadeClassifier("models/haarcascade_frontalface_default.xml")
filename = "images/got.jpg"
filename_compressed = "output/compressed.jpg"
filename_fpeg = "output/fpeg.jpg"
BACKGROUND_QUALITY = 10
FACE_QUALITY = 75
# load source and compressed version
source = cv2.imread(filename)
cv2.imwrite(filename_compressed, source, [cv2.IMWRITE_JPEG_QUALITY, BACKGROUND_QUALITY])
compressed = cv2.imread(filename_compressed)
# find faces as rects
faces = cascade.detectMultiScale(source, scaleFactor = 1.1,
minNeighbors = 5, minSize=(30,30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
print "found {} faces!".format(faces.size/4)
# move faces from source to compressed source
for (x, y, w, h) in faces:
compressed[y:(y + h), x:(x + w)] = source[y:(y + h), x:(x + w)]
cv2.rectangle(compressed, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imwrite(filename_fpeg, compressed, [cv2.IMWRITE_JPEG_QUALITY, FACE_QUALITY])