0

I'm trying to get a video feed through camera of my android phone using IP webcam and opencv.I'm using python 3.5(anaconda distribution) with opencv 3.1.0 Here is the code:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
cap.open('http://192.168.1.4:8080/video')
while True:
    ret, frame = cap.read()
    cv2.imshow('RGB output', frame)
    if cv2.waitKey(0):
        break
cap.release()
cv2.destroyAllWindows()

Problem is that when I run the script, I can only see the first frame rather than the constant video feed. I've searched alot but couldn't find a solution. Help!

Amol Borkar
  • 2,321
  • 7
  • 32
  • 63
  • 1
    problem: if you press any key you will break. but since you have waitKey (0) you'll have to press a key. So change to sth. like if waitkey (0) == 'q' to only stop when q is pressed or change to if waitKey(10) to not wait for a keypress but stream continuously and only cancel on keyPress – Micka Jan 08 '16 at 18:02
  • What a silly mistake by me! It now works flawlessly, Thanks! – Amol Borkar Jan 09 '16 at 01:54

2 Answers2

0
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
cap.open('http://192.168.1.4:8080/video')

while True:
    ret, frame = cap.read()
    cv2.imshow('RGB output', frame)
    key = cv2.waitKey(1):
    if key == ('c'):
            break

cap.release()
cv2.destroyAllWindows()

Try the above code it will be just fine..

barbsan
  • 3,418
  • 11
  • 21
  • 28
0
cap=cv2.VideoCapture(0)
while True:
    success,frame=cap.read()  
    if(success):
        gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        cv2.imshow("Image",gray)
    
    
    if cv2.waitKey(1) & 0xFF==ord('z'):
        break
        
        
        
cap.release()        
cv2.destroyAllWindows()

cv2.waitKey(0) is the reason you have to set it to 1 if you want to close The Window from the existing button leave your code as it is but without 0 to give time to CPU to process since it Video.

Notice, You have to add & 0xFF To Avoid other problems occurring if your NumLock is activated.

More Here What's 0xFF for in cv2.waitKey(1)?

This Code Above is the Best Practice

Mohamed Fathallah
  • 1,274
  • 1
  • 15
  • 17