Go to Post For Pete's sake, don't concentrate on the entry itself... concentrate on what your team is doing to make an impact in your community. - Andy Baker [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 25-05-2016, 13:53
hamza.mahdi hamza.mahdi is offline
Registered User
no team
 
Join Date: May 2016
Location: Toronto
Posts: 2
hamza.mahdi is an unknown quantity at this point
Python IP camera

Hello,

I am using an axis camera and python to do visual tracking. I have a problem with the set up. There is no problem with the code interpreting. This is my first time using an IP camera so i'm not sure if I'm even doing it correctly.

Here's my code(spacing is correct on my computer)

import numpy as np
import cv2
cap = cv2.VideoCapture()
cap.open("http://169.254.197.26/")

while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()

# Our operations on the frame come here
print "hi"

# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Last edited by hamza.mahdi : 25-05-2016 at 13:54. Reason: code spacing
Reply With Quote
  #2   Spotlight this post!  
Unread 25-05-2016, 15:06
kylelanman's Avatar
kylelanman kylelanman is offline
Programming Mentor
AKA: Kyle
FRC #2481 (Roboteers)
Team Role: Mentor
 
Join Date: Feb 2008
Rookie Year: 2007
Location: Tremont Il
Posts: 185
kylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to all
Re: Python IP camera

I don't think this code works. If it does then we really over complicated the way we did it.
Code:
cap = cv2.VideoCapture()
cap.open("http://169.254.197.26/")
I just made this repo public. We haven't officially cleaned it up and done our 2016 code release but it provides an example of how to read images from an IP camera in python.

https://github.com/Frc2481/paul-buny...Camera/main.py
__________________
"May the coms be with you"

Is this a "programming error" or a "programmer error"?

Reply With Quote
  #3   Spotlight this post!  
Unread 25-05-2016, 15:18
hamza.mahdi hamza.mahdi is offline
Registered User
no team
 
Join Date: May 2016
Location: Toronto
Posts: 2
hamza.mahdi is an unknown quantity at this point
Re: Python IP camera

Thanks for your reply,

I tried your program but it's missing a library networktables

I'm getting this error
Traceback (most recent call last):
File "C:/Users/Hamza/Pictures/chiefdelphi/main.py", line 5, in <module>
from networktables import NetworkTable
ImportError: No module named networktables
Reply With Quote
  #4   Spotlight this post!  
Unread 25-05-2016, 16:15
kylelanman's Avatar
kylelanman kylelanman is offline
Programming Mentor
AKA: Kyle
FRC #2481 (Roboteers)
Team Role: Mentor
 
Join Date: Feb 2008
Rookie Year: 2007
Location: Tremont Il
Posts: 185
kylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to all
Re: Python IP camera

Using it verbatim might not be the best idea. You can extract the portion that actually reads the image from the camera.

Regardless the README for the repo details how to install networktables https://github.com/Frc2481/paul-bunyan
__________________
"May the coms be with you"

Is this a "programming error" or a "programmer error"?

Reply With Quote
  #5   Spotlight this post!  
Unread 25-05-2016, 16:34
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,032
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Python IP camera

Your URL is incorrect, you need to point at the stream page (I forget the particular URL). Additionally, your OpenCV needs to be linked to libav/ffmpeg for open-by-url to work correctly.

I wouldn't recommend doing it that way anyways, as libav/ffmpeg introduces significant latency because of the way that the stream is decoded (as of the last time I checked, which was 2 years ago).
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
Reply With Quote
  #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
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 08:56.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi