popeize/popeize.py
2015-02-21 18:32:43 +01:00

61 lines
2 KiB
Python

import cv2
import sys
def blit(l_img, s_img, x_offset, y_offset):
for c in range(0, 3):
l_img[y_offset:y_offset+s_img.shape[0], x_offset:x_offset+s_img.shape[1], c] = s_img[:,:,c] * (s_img[:,:,3]/255.0) + l_img[y_offset:y_offset+s_img.shape[0], x_offset:x_offset+s_img.shape[1], c] * (1.0 - s_img[:,:,3]/255.0)
def downscale(img, width, height):
factor = min(1.0*height/img.shape[0], 1.0*width/img.shape[1])
return cv2.resize(img, None, None, factor, factor)
def popeize(faceCascade, image, papaj, faceScale=1.5):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(gray, faceScale, 5)
print "Found {0} faces!".format(len(faces))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
factor = min(1.0*h/papaj.shape[0], 1.0*w/papaj.shape[1]) * 1.3
scaledpapaj = cv2.resize(papaj, None, None, factor, factor)
dx = x + (w/2) - scaledpapaj.shape[1]/2
dy = y + (h/2) - scaledpapaj.shape[0]/2
print dx, dy, factor
try:
blit(image, scaledpapaj, dx, dy)
except Exception as e:
print e, dx, dy, factor
#cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
if __name__ == '__main__':
imagePath = sys.argv[1]
cascPath = sys.argv[2]
# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
# Read the image
image = cv2.imread(imagePath)
papaj = cv2.imread("zoltamorda.png", -1)
cv2.namedWindow("image")
def trackbarChanged(value):
image = cv2.imread(imagePath)
popeize(faceCascade, image, papaj, 1.1 + value/100.0)
cv2.imshow("image", downscale(image, 1024, 768))
print 'showed'
cv2.createTrackbar("scale", "image", 0, 300, trackbarChanged)
popeize(faceCascade, image, papaj, 1.1)
cv2.imwrite("output.jpg", image)
cv2.imshow("image", downscale(image, 1024, 768))
while cv2.waitKey(0) & 0xff != ord('q'):
pass