Ask Your Question
0

how to read video from webcam with opencv3.3 and python3.5 on ubuntu 16.04?

asked 2017-11-18 08:05:47 -0500

soro gravatar image

hello i have some problem to read video from camera using pythom3 and opencv3.3 when i use python2.7 the code give me the video but when i change the interpreter to python3.5 yhe code is build but no error no result , it failt to open the camera here is the test code.

import numpy as np

import cv2

cap = cv2.VideoCapture(0)

while(cap.isOpened()): # check ! # capture frame-by-frame ret, frame = cap.read()

if ret: # check ! (some webcam's need a "warmup")
    # our operation on frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame', gray)

if cv2.waitKey(1) & 0xFF == ord('q'):
    break

When everything is done release the capture

cap.release() cv2.destroyAllWindows()

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-11-18 08:38:50 -0500

supra56 gravatar image
import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

The cv2.waitKey could be inside if/else condition block

supra56 gravatar imagesupra56 ( 2017-11-18 08:45:47 -0500 )edit

yes i tried hall these cases but nothing . it termiates immediately it goes immediately to release

soro gravatar imagesoro ( 2017-11-18 09:29:45 -0500 )edit

I want u to test this to see if it work or not..

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
supra56 gravatar imagesupra56 ( 2017-11-18 10:26:19 -0500 )edit

Btw, Used python 3.5 and OpenCV 3.3.x

supra56 gravatar imagesupra56 ( 2017-11-18 10:27:55 -0500 )edit

thank it work on my laptop but not the destop. it should be due to hardware configuration thank you very much

soro gravatar imagesoro ( 2017-11-18 10:41:02 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-18 08:05:47 -0500

Seen: 2,456 times

Last updated: Nov 18 '17