Skip to content

Instantly share code, notes, and snippets.

@shihyuan
Last active November 25, 2022 18:54
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save shihyuan/4d834d429763e953a40c to your computer and use it in GitHub Desktop.
Save shihyuan/4d834d429763e953a40c to your computer and use it in GitHub Desktop.
Stream Video with OpenCV in Python from an Android running IP Webcam
# Stream Video with OpenCV from an Android running IP Webcam (https://play.google.com/store/apps/details?id=com.pas.webcam)
# Code Adopted from http://stackoverflow.com/questions/21702477/how-to-parse-mjpeg-http-stream-from-ip-camera
import cv2
import urllib2
import numpy as np
import sys
host = "192.168.0.220:8080"
if len(sys.argv)>1:
host = sys.argv[1]
hoststr = 'http://' + host + '/video'
print 'Streaming ' + hoststr
stream=urllib2.urlopen(hoststr)
bytes=''
while True:
bytes+=stream.read(1024)
a = bytes.find('\xff\xd8')
b = bytes.find('\xff\xd9')
if a!=-1 and b!=-1:
jpg = bytes[a:b+2]
bytes= bytes[b+2:]
i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
cv2.imshow(hoststr,i)
if cv2.waitKey(1) ==27:
exit(0)
@sezan92
Copy link

sezan92 commented Feb 16, 2015

Thank you very much.. I searched web so much.. nothing worked [even the given link in the comments] , but this one ..thank you!

@narendiran1996
Copy link

Thank you very much..

@justinsinger
Copy link

Bless this gist!

@nourlikic
Copy link

The code below will also work

pipe = sp.Popen([ "ffmpeg",
           "-i", URL,
           "-loglevel", "quiet",
           "-an",
           "-f", "image2pipe",
           "-pix_fmt", "bgr24",
           "-vcodec", "rawvideo", "-"],
           stdin = sp.PIPE, stdout = sp.PIPE)

while True:
    raw_image = pipe.stdout.read(width*height*3)
    image = np.frombuffer(raw_image, dtype=np.uint8).reshape((height,width,3))
    cv2.imshow("xyz",image)
    if cv2.waitKey(5) == 27:
        break

@mrlunk
Copy link

mrlunk commented May 25, 2017

That can be done a lot simpler...
https://pastebin.com/7izkYjRx

@amirmehdi
Copy link

amirmehdi commented Nov 29, 2017

does it have seek bar?
and can get this by call api from mobile app?

@aashish-bth
Copy link

Is there any way to achieve the same using request because I m using python 3.x and urllib2 not available for Python 3.x

@dhiraj262
Copy link

Here is the code for Python 3.x

import urllib
import cv2
import numpy as np
url='http://192.168.8.22:8080/shot.jpg'

while True:

# Use urllib to get the image and convert into a cv2 usable format
imageResp=urllib.request.urlopen(url)
imgNp=np.array(bytearray(imageResp.read()),dtype=np.uint8)
img=cv2.imdecode(imgNp,-1)

# put the image on screen
cv2.imshow('IPWebcam',img)

#To give the processor some less stress


if cv2.waitKey(1) ==13:
    break

cv2.destroyAllWindows()

@albertoisorna
Copy link

Is this possible from remote?? To put the info in a cloud machine and get the info from there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment