Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv using python and flask : HIGHGUI ERROR: libv4l unable to ioctl S_FMT

I am trying to place an image over a webcam feed in camera.py and send to main.py; output displayed in a flask generated local server . But I encountered 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):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        self.video = cv2.VideoCapture(0)
        # If you decide to use video.mp4, you must have this file in the folder
        # as the main.py.
        # self.video = cv2.VideoCapture('video.mp4')

    def __del__(self):
        self.video.release()

    def get_frame(self):
        success, frame = self.video.read()
        # We are using Motion JPEG, but OpenCV defaults to capture raw images,
        # so we must encode it into JPEG in order to correctly display the
        # 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 the overlay image: glasses.png
    imgGlasses = cv2.imread('4.png', -1)

    print imgGlasses is None

    # Create the mask for the 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 the inverted mask for the glasses
    orig_mask_inv = cv2.bitwise_not(orig_mask)
    #cv2.imwrite("orig_mask_inv.png", orig_mask_inv)

    # Convert glasses image to BGR
    # and save the original image size (used later when re-sizing the 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)

    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

            # Center the glasses
            x1 = ex - 15
            x2 = ex + ew + 15
            y1 = ey - 5
            y2 = ey + eh + 15

            # Check for clipping
            if x1 < 0:
                x1 = 0
            if y1 < 0:
                y1 = 0
            if x2 > w:
                x2 = w
            if y2 > h:
                y2 = h

            # Re-calculate the width and height of the glasses image
            glassesWidth = x2 - x1
            glassesHeight = y2 - y1

            # Re-size the original image and the masks to the 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 for glasses from background equal to size of glasses image
            roi = roi_color[y1:y2, x1:x2]

            # roi_bg contains the original image only where the glasses is not
            # in the region that is the size of the glasses.
            roi_bg = cv2.bitwise_and(roi,roi,mask = mask)

            # roi_fg contains the image of the glasses only where the glasses is
            roi_fg = cv2.bitwise_and(glasses,glasses,mask = mask_inv)

            # join the roi_bg and roi_fg
            dst = cv2.add(roi_bg,roi_fg)

            # place the joined image, saved to dst back over the 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>
like image 369
anand Avatar asked May 08 '15 04:05

anand


1 Answers

Try removing debug=True in the main.py. That was causing the problem for me.

like image 64
William Maio Avatar answered Nov 15 '22 06:11

William Maio