Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it possible to use a RSTP camera instead of the piCam #14

Open
masterchop opened this issue May 14, 2018 · 9 comments
Open

is it possible to use a RSTP camera instead of the piCam #14

masterchop opened this issue May 14, 2018 · 9 comments

Comments

@masterchop
Copy link

I wonder if we could use network camera to process the computer vision from the Pi

@PietjeNL
Copy link

Jeah that would be a great example to add to it. Have a old dome camera laying around that gives to much false positives.

@masterchop
Copy link
Author

i have been trying to port this code but i am not sure how to modify from picam to rstp.
I can do RSTP on python with opencv but when i run face recognition or any other it goes super slow i just cant find out what he did on this one to make that fast. i am new to python :(

@masterchop
Copy link
Author

I tried to port this code into the camera.py code unsuccesfully:

`class ipCamera(object):

def __init__(self, url, user=None, password=None):
    self.url = url
    auth_encoded = base64.encodestring('%s:%s' % (user, password))[:-1]

    self.req = urllib2.Request(self.url)
    self.req.add_header('Authorization', 'Basic %s' % auth_encoded)

def get_frame(self):
    response = urllib2.urlopen(self.req)
    img_array = np.asarray(bytearray(response.read()), dtype=np.uint8)
    frame = cv2.imdecode(img_array, 1)
    return frame`

@masterchop
Copy link
Author

i had to remove some stuff of the code but got it. Thanks

@TristanMW
Copy link

@masterchop Please share your code. I have a IP camera and plan to use the rtsp stream aswell

Thanks

@masterchop
Copy link
Author

Oh wow that was 2 years ago, I'll have to dig on my stuff to see If i can find it.

@TristanMW
Copy link

haha thanks

@masterchop
Copy link
Author

This what i end up using:

`import cv2
import traceback
from threading import Thread, Lock

class WebcamVideoStream :

def __init__(self, src) :
    self.daemon = True  # Not sure if i shuld remove this one.https://stackoverflow.com/questions/12493467/python-threading-threads-do-not-close
    self.stream = cv2.VideoCapture(src)
    #self.stream.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, width)
    #self.stream.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, height)
    (self.grabbed, self.frame) = self.stream.read()
    self.started = False
    self.read_lock = Lock()

def start(self) :
    if self.started :
        print("[INFO] already started!!")
        return None
    self.started = True
    self.thread = Thread(target=self.update, args=())
    self.thread.start()
    return self

def update(self) :
    while self.started :
        (grabbed, frame) = self.stream.read()
        self.read_lock.acquire()
        self.grabbed, self.frame = grabbed, frame
        self.read_lock.release()

def read(self) :
    try:
       self.read_lock.acquire()
       frame = self.frame.copy()
       self.read_lock.release()
       return frame
    except Exception as e:
       self.read_lock.release() # para ver si quitamos el lock.
       print("[ERROR] Camera Disconnected")
       print(e)# this is the reason why it fails could be the lock or frame.
       return None

#added to send the emails.
def get_frame(self):
frame = self.frame.copy()
ret, jpeg = cv2.imencode('.jpg', frame)
return jpeg.tobytes()

def stop(self) :
    try:
       self.started = False
       #self.thread.join()
    except Exception as e:
       print("[ERROR: Stop CameraClass Failed..]")

def __exit__(self, exc_type, exc_value, traceback) :
    self.stream.release()`

@TristanMW
Copy link

Thank You. Will test it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants