i trying place image on webcam feed in camera.py , send main.py; output displayed in flask generated local server . encountered following error
libv4l2: error setting pixformat: device or resource busy highgui error: libv4l unable ioctl s_fmt libv4l2: error setting pixformat: device or resource busy libv4l1: error setting pixformat: device or resource busy highgui error: libv4l unable ioctl vidiocspict
i used following code:
main.py
from flask import flask, render_template, response camera import videocamera app = flask(__name__) @app.route('/') def index(): return render_template('index.html') def gen(camera): while true: frame = camera.get_frame() yield (b'--frame\r\n' b'content-type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n') @app.route('/video_feed') def video_feed(): return response(gen(videocamera()), mimetype='multipart/x-mixed-replace; boundary=frame') if __name__ == '__main__': app.run(host='0.0.0.0', debug=true)
camera.py
import cv2, time import numpy np class videocamera(object): def __init__(self): # using opencv capture device 0. if have trouble capturing # webcam, comment line below out , use video file # instead. self.video = cv2.videocapture(0) # if decide use video.mp4, must have file in folder # main.py. # self.video = cv2.videocapture('video.mp4') def __del__(self): self.video.release() def get_frame(self): success, frame = self.video.read() # using motion jpeg, opencv defaults capture raw images, # must encode jpeg in order correctly display # video stream. #time.sleep(.1) face_cascade = cv2.cascadeclassifier('haarcascades/haarcascade_frontalface_default.xml') eye_cascade = cv2.cascadeclassifier('haarcascades/haarcascade_mcs_eyepair_small.xml') # load overlay image: glasses.png imgglasses = cv2.imread('4.png', -1) print imgglasses none # create mask glasses imgglassesgray = cv2.cvtcolor(imgglasses, cv2.color_bgr2gray) #cv2.imwrite("imgglassesgray.png", imgglassesgray) ret, orig_mask = cv2.threshold(imgglassesgray, 0, 255, cv2.thresh_binary) #cv2.imwrite("orig_mask.png", orig_mask) # create inverted mask glasses orig_mask_inv = cv2.bitwise_not(orig_mask) #cv2.imwrite("orig_mask_inv.png", orig_mask_inv) # convert glasses image bgr # , save original image size (used later when re-sizing image) imgglasses = imgglasses[:,:,0:3] origglassesheight, origglasseswidth = imgglasses.shape[:2] video_capture = cv2.videocapture(0) #while true: #ret, frame = video_capture.read() gray = cv2.cvtcolor(frame, cv2.color_bgr2gray) faces = face_cascade.detectmultiscale(gray, 1.3, 5, flags=cv2.cv.cv_haar_scale_image) (x,y,w,h) in faces: cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2) roi_gray = gray[y:y+h, x:x+w] roi_color = frame[y:y+h, x:x+w] eyes = eye_cascade.detectmultiscale(roi_gray) (ex,ey,ew,eh) in eyes: cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),1) (ex, ey, ew, eh) in eyes: glasseswidth = 3*ew glassesheight = glasseswidth * origglassesheight / origglasseswidth # center glasses x1 = ex - 15 x2 = ex + ew + 15 y1 = ey - 5 y2 = ey + eh + 15 # check clipping if x1 < 0: x1 = 0 if y1 < 0: y1 = 0 if x2 > w: x2 = w if y2 > h: y2 = h # re-calculate width , height of glasses image glasseswidth = x2 - x1 glassesheight = y2 - y1 # re-size original image , masks glasses sizes # calcualted above glasses = cv2.resize(imgglasses, (glasseswidth,glassesheight), interpolation = cv2.inter_area) mask = cv2.resize(orig_mask, (glasseswidth,glassesheight), interpolation = cv2.inter_area) mask_inv = cv2.resize(orig_mask_inv, (glasseswidth,glassesheight), interpolation = cv2.inter_area) # take roi glasses background equal size of glasses image roi = roi_color[y1:y2, x1:x2] # roi_bg contains original image glasses not # in region size of glasses. roi_bg = cv2.bitwise_and(roi,roi,mask = mask) # roi_fg contains image of glasses glasses roi_fg = cv2.bitwise_and(glasses,glasses,mask = mask_inv) # join roi_bg , roi_fg dst = cv2.add(roi_bg,roi_fg) # place joined image, saved dst on original image roi_color[y1:y2, x1:x2] = dst break ret, jpeg = cv2.imencode('.jpg', frame) return jpeg.tobytes()
index.html
<html> <head> <title>video streaming demonstration</title> <link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='styles.css')}}" /> <style> body { background-image: url(http://cdn.wall88.com/51b487f75df1050061.jpg); background-repeat: no-repeat; } </style> </style> </head> <body> <h1>video streaming demonstration</h1> <img id="bg" align="middle" src="{{ url_for('video_feed') }}"> </body> </html>
try removing debug=true
in main.py
. causing problem me.
Comments
Post a Comment