Play video Basic - python

im new to python openCV,found this code from openCV page;
import cv2
cap = cv2.VideoCapture('Megamind.avi')
while (cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('MMR3', gray)
if cv2.waitkey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()*
Tried to run it but it gives error which after done some digging up, i replaced this line:
"cap = cv2.VideoCapture('Megamind.avi')" with this line:
"cap = cv2.VideoCapture('Megamind.avi', cv2.CAP_FFMPEG)"
The program run without error but the video windows("MMR3") was not displayed.
**Im using python 2.7.13 with opencv3 running on MacOs Sierra.
**Megamind.avi is available in the same folder where the code is

your cv2.imshow has to be in the whille loop. If your cv2.imshow() is outside while loop than it will only display the last frame of you video. Change you code to below code
import cv2
cap = cv2.VideoCapture('Megamind.avi')
while (cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('MMR3', gray)
if cv2.waitkey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Related

How could i play a local video file in python?

The following steps are to be carried out:
pick video file
and play it
tried the following code, but didn't pick video file
import numpy as np
import cv2
cap = cv2.VideoCapture('E:\Moives\Avengers.Infinity.War.mkv')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
You could use vlc module for python maybe that can solve your question

OpenCV error with detectMultiScale in python

when I start my program on vscode I got this error :
enter image description here
Whereas when I execute the .py the program work well. I don't understand why vscode don't run the the python file. Here is my code:
import cv2
img = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier("cascade.xml")
while(True):
ret, frame = img.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,1.3,4)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()

Python VideoCapture(0) my camera bad work

This my codes :
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()
I write the code like this and try to open my camera, but the camera image looks like the screenshot.
Stop working screen shot
Any idea ? What is the reason?
It's indentation problem.
Try this code:
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()
Provide proper indentation so the program works fine.
Press "q" to stop the program.

I can't see the videocapture

i am trying to learn some image processing using openCV and i wrote a basic code but the "frame" windows won't show.I've tried installing on a separate computer but i get the same problem.
import cv2
cap = cv2.VideoCapture(0);
while (True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Based on the code you added your if statement is outside the while loop and therefore can never be checked. It should have thrown a different error however if that was the case. Your camera might be detected but not working. Try using it like a traditional webcam on another program like camera or photobooth to help troubleshoot the issue.
import cv2
cap = cv2.VideoCapture(0);
while (True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

error with cv2.destroyAllWindws() on ubuntu 14.04

I'm programing in python and opencv on Ubuntu but the cv2.destoyAllWindows() doesn't seem to work,as in this example:
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()
when i press 'q' the program end but the window remain on the screen how ever on windows i didn't face such thing
any help I will be thankful

Categories

Resources