I am trying to put an image through a webcam channel in camera.py and send to main.py; output displayed on the local server created by the jar. But I ran into the following error
libv4l2: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl VIDIOCSPICT
I used the following code:
main.py
from flask import Flask, render_template, Response
from 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 as np
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
def __del__(self):
self.video.release()
def get_frame(self):
success, frame = self.video.read()
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_mcs_eyepair_small.xml')
imgGlasses = cv2.imread('4.png', -1)
print imgGlasses is None
imgGlassesGray = cv2.cvtColor(imgGlasses, cv2.COLOR_BGR2GRAY)
ret, orig_mask = cv2.threshold(imgGlassesGray, 0, 255, cv2.THRESH_BINARY)
orig_mask_inv = cv2.bitwise_not(orig_mask)
imgGlasses = imgGlasses[:,:,0:3]
origGlassesHeight, origGlassesWidth = imgGlasses.shape[:2]
video_capture = cv2.VideoCapture(0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5, flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
for (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)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)
for (ex, ey, ew, eh) in eyes:
glassesWidth = 3*ew
glassesHeight = glassesWidth * origGlassesHeight / origGlassesWidth
x1 = ex - 15
x2 = ex + ew + 15
y1 = ey - 5
y2 = ey + eh + 15
if x1 < 0:
x1 = 0
if y1 < 0:
y1 = 0
if x2 > w:
x2 = w
if y2 > h:
y2 = h
glassesWidth = x2 - x1
glassesHeight = y2 - y1
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)
roi = roi_color[y1:y2, x1:x2]
roi_bg = cv2.bitwise_and(roi,roi,mask = mask)
roi_fg = cv2.bitwise_and(glasses,glasses,mask = mask_inv)
dst = cv2.add(roi_bg,roi_fg)
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>