Last time I tried calling a Java program from bottle.py, but when accessed with a browser by the same method, I wrote a server that acquires a webcam image, recognizes a face image with OpenCV and returns the number of people in front of the camera saw.
Last time I wrote it in Python3, but this time it's Python2.7 and it's awkward to write import cv, cv2. .. ..
from bottle import route, run, template
import cv,cv2
import time
@route('/cv')
def index():
cnt = 0
cap = cv2.VideoCapture(0)
time.sleep(0.1)
ret,im = cap.read()
cv2.imwrite("camera.jpg ",im)
cap.release()
cascade = cv.Load("haarcascade_frontalface_default.xml")
cvim = cv.LoadImage("camera.jpg ")
faces = cv.HaarDetectObjects(cvim, cascade, cv.CreateMemStorage())
for (x,y,w,h),n in faces:
cnt = cnt + 1
cv.Rectangle(cvim, (x,y), (x+w,y+h), 255)
cv.SaveImage('dst.jpg',cvim)
return template('{{cnt}}', cnt=cnt)
run(host='127.0.0.1', port=8080)
Recommended Posts