I'm trying to connect my phone camera using IP webcam application but I get when error when I run the code.
Also, the URL keeps changing. Is there a method so that I don't need to change the URL every time?
This is the code I am using:
import cv2
cap = cv2.VideoCapture("http://192.168.43.1:8080/shot.jpg")
while True:
ret, frame = cap.read()
cv2.imshow("IPWebcam", cv2.resize(frame, (600, 400)))
if cv2.waitKey(20) & 0xFF == ord('q'):
break
And this is the error message I get when I run it:
Traceback (most recent call last):
File ".\phone_cam.py", line 15, in <module>
cv2.imshow("IPWebcam", cv2.resize(frame, (600, 400)))
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-9gpsewph\opencv\modules\imgproc\src\resize.cpp:3929: error:
(-215:Assertion failed) !ssize.empty() in function 'cv::resize'
This answer will not directly solve the issue, but it will allow you to detect the cause and it's a good practice when reading a video, image or from a camera. Always check if the value of ret is True because if it is not, it means there was a problem reading the data.
import cv2
cap = cv2.VideoCapture("http://192.168.43.1:8080/shot.jpg")
while True:
ret, frame = cap.read()
if ret:
cv2.imshow("IPWebcam", cv2.resize(frame, (600, 400)))
if cv2.waitKey(20) & 0xFF == ord('q'):
break
else:
print("cap.read() returned False")
If the code prints the message in the else statement, that means there is an issue with the link. Check if it is the correct one and whether you need to add a username and password or not.
import cv2
import urllib.request
import numpy as np
URL = "http://192.168.43.1:8080/shot.jpg"
while(True):
img_arr = np.array(
bytearray(urllib.request.urlopen(URL).read()), dtype=np.uint8)
frame = cv2.imdecode(img_arr, -1)
# Display the image
cv2.imshow('IPWebcam', cv2.resize(frame, (1100, 800)))
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cv2.release()
cv2.destroyAllWindows()
Related
I am trying to enable the IP cam that I have access to, it's feed by the browser. but couldn't able to get a video stream using an IP camera, only get a result in image form. The feed with cv2.videocapture() gives an error.
import cv2
import requests
import numpy as np
from hikvisionapi import Client
cam = Client('http://*.*.*.*', '****', '******', timeout=10)
vid = cam.Streaming.channels[102].picture(method ='get', type = 'opaque_data')
cap = cv2.VideoCapture(vid)
# Check if the webcam is opened correctly
if not cap.isOpened():
raise IOError("Cannot open webcam")
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
cv2.imshow('Input', frame)
c = cv2.waitKey(1)
if c == 27:
break
cap.release()
cv2.destroyAllWindows()
by using the below code the error is resolved
ret, frame = cap.read()
#print('About to show frame of Video.')
cv2.imshow("Capturing for facial recog",frame)
I am learning image processing using python and opencv
I write this code in python
import numpy as np
import cv2
vidCap=cv2.VideoCapture('output.avi')
print('before while')
while(vidCap.isOpened()):
print('inside while')
ret, frame=vidCap.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF==ord('q'):
break
print('outside while')
vidCap.release()
cv2.destroyWindow('LoadedVideo')
and it is giving me this error
Traceback (most recent call last):
File "D:\Python Image
Processing\FirstExercise\PlayingVideoFromFile.py", line 12, in <module>
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
error: C:\builds\master_PackSlaveAddon-win32-vc12-
static\opencv\modules\imgproc\src\color.cpp:7456: error: (-215) scn == 3 ||
scn == 4 in function cv::ipp_cvtColor
The code is passing None as the frame to cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) and this is causing the exception.
You should check the return value of vidCap.read(); if it returns False as the first item in the tuple then there was no grabbed frame and you should not call cv2.cvtColor() on it because its value will be None.
vidCap.isOpened() will continue to return True even after all the frames have been consumed, so it should not be used as the condition in the while loop. The loop could be written as:
if vidCap.isOpened():
while True:
ret, frame = vidCap.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', frame)
if (cv2.waitKey(1) & 0xFF) == ord('q'):
break
else:
break
vidCap.release()
Now the loop is exited when there are no more frames to grab from the file or a 'q' key press is detected.
Sorry I paste mirror parts at worng place, and as frame is null during mirror part, I made it into a comment while running
Looks like my cap.read() can't read anything
This is really a simple python code of opencv for just capturing a window and make webcamera work. But when I ran it, no window shown but no bugs shown either. How can I know what's wrong readily?
How can I know if the webcamera is 0 or sth else?
Why the mirror part doesn't work?
Could someone recommend me some good examples for opencv on python?
Thank you!!
import cv2
def capture_camera(mirror=True, size=None):
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
#if mirror is True:
#frame = frame[:,::-1]
if size is not None and len(size) == 200:
frame = cv2.resize(frame, size)
cv2.imshow('camera capture', frame)
k = cv2.waitKey(100)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
capture_camera()
Your code is not executiong below if statement
if size is not None and len(size) == 200:
because size is tuple and len(size) will return 2 since you defined
size = (800,600)
2nd mistake : you were changing frame array before it is initialized.
first you need to get frame array which is returned by cap.read() then you can check if mirror is True if yes then reverse the array by frame = frame[:,::-1]
try this code :
import cv2
def capture_camera(mirror=True, size=None):
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if mirror is True:
frame = frame[:,::-1]
size = (800,600)
if size is not None:
frame = cv2.resize(frame, size)
cv2.imshow('camera capture', frame)
k = cv2.waitKey(100)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
capture_camera()
Try this code. It works for me:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('Webcam (close with q)' ,frame)
if(cv2.waitKey(1) & 0xFF == ord('q')):
break
cap.release()
cv2.destroyAllWindows()
BTW:
Your code throws an error:
Traceback (most recent call last):
File "C:/Python/Scripts/OpenCV/Scripts/so_test.py", line 21, in <module>
capture_camera()
File "C:/Python/Scripts/OpenCV/Scripts/so_test.py", line 7, in capture_camera
frame = frame[:,::-1]
UnboundLocalError: local variable 'frame' referenced before assignment
The program needs to read a video, do the counting of frames and then show in a window. The error occurs after the video ends.
How can i solve this?
Here is the source:
import tkMessageBox
import cv2
banner = cv2.imread('../data/banner.png')
video = cv2.VideoCapture('../data/pintado_real_crop.avi')
contadorDeFrames = True
contador = 0
cv2.imshow('Projeto Pacu', banner)
cv2.moveWindow('Projeto Pacu', 100,50)
while(contadorDeFrames == True):
contadorDeFrames, frame = video.read()
contador = contador + 1
cv2.imshow("Video", frame)
cv2.moveWindow('Video', 100, 178)
print"Frame: %d" %contador
k = cv2.waitKey(30) & 0xff
if k == 27:
break
tkMessageBox.showinfo("Frames contador: %d" %contador)
video.release()
cv2.destroyAllWindows()
Complete Error:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp, line 269
Traceback (most recent call last):
File "/home/vagner/PycharmProjects/TesteVideo/src/TestaVideo.py", line 20, in <module>
cv2.imshow("Video", frame)
cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp:269: error: (-215) size.width>0 && size.height>0 in function imshow
I donĀ“t have an answer for the question but i can specify from where the error came and i have a workaround.
Like you see in my first example i tried to print a quit in the if statement and noticed that it doesnt get called. So i showed how much Frames the video had and let the if statement get called by the framenumbers. So the Error Disapear.
Example 1:
import numpy as np
import cv2
cap = cv2.VideoCapture('richtigcounter.mp4')
frames =1
while True:
ret, frame = cap.read()
print ("test")
cv2.imshow('frame',frame)
frames =frames + 1
print (frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
print("Quit")
break
cap.release()
cv2.destroyWindow(cap)
Example 2:
import numpy as np
import cv2
import time
def Reinfall():
cap = cv2.VideoCapture('reinfallcounter.mp4')
frames =1
while True:
ret, frame = cap.read()
cv2.imshow('video', frame)
frames =frames + 1
print (frames)
cv2.waitKey(1)
if frames == 245:
print("Quit")
break
cap.release()
cv2.destroyAllWindows()
In your code, you are trying to print the null frame:
contadorDeFrames, frame = video.read()
contador = contador + 1
cv2.imshow("Video", frame)
when your code reads the frame which is last frame+1, the value will be null and you are trying to print that frame. So check whether the frame is null or not and print.
I wrote the following code to open a video file and the file is in the same directory as the script, moreover the code to write a video feed from the camera to the file is not working!
import numpy as np
import cv2
cap = cv2.VideoCapture('F:/vtest.avi')
print cap.isOpened()
if(cap.isOpened()== False):
cap.open('F://vtest.avi')
print cap.isOpened()
while(cap.read()):
ret,frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
But the code threw the following errors. I tried moving the ffmpeg.dll file in Python directory but to no avail.
False
False
Traceback (most recent call last):
File "F:\2.py", line 11, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3480: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
Try this:
import numpy as np
import cv2
cap = cv2.VideoCapture('F:/vtest.avi')
while(cap.isOpened()):
ret,frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Also, place the video in the same directory as this script and check if it works.