View Single Post
  #6   Spotlight this post!  
Unread 25-05-2016, 19:51
and3212's Avatar
and3212 and3212 is offline
Registered User
FRC #0401 (Copperhead Robotics)
Team Role: Programmer
 
Join Date: Dec 2015
Rookie Year: 2015
Location: Blacksburg
Posts: 3
and3212 is an unknown quantity at this point
Re: Python IP camera

We also decided to use an AXIS IP Camera for this years game. For connecting to our IP Camera we used the following code. Just don't forget to change the IP Address that we have to the one of your camera as it is most likely different.

Code:
import cv2
import requests
import logging
import sys
from time import sleep

logging.basicConfig(level=logging.DEBUG)

if len(sys.argv) != 2:
    print("Error: specify an IP to connect to!")
    exit(0)

ip = sys.argv[1]

#Wait for IP camera to connect. Will not exit until this works
while(True):
    try:
        url = 'http://10.4.1.19/mjpg/video.mjpg' # IP Address of the Camera
        stream = requests.get(url, stream=True)
        bytes = b''
        print('Connected to IP Camera')
        break
    except:
        sleep(0.5)
        print('No cam yet')
        pass
    

while(True):

    # When nothing is seen there is a divide by zero error, so this skips over those frames
    try:
        # Takes frames from the camera that we can use
        bytes+=stream.raw.read(16384)
        a = bytes.find(b'\xff\xd8')
        b = bytes.find(b'\xff\xd9')
        if a!=-1 and b!=-1:
            jpg = bytes[a:b+2]
            bytes= bytes[b+2:]
            frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
            img = frame
        
        # Continue code here.....
We had trouble normally connecting to the IP Camera using just OpenCV so we decided to use the requests module. I assume you are using Python 2.7 because of your print statements but if you would like the syntax for Python 3 just message me.
Reply With Quote