quatanium / python-onvif

ONVIF Client Implementation in Python
MIT License
469 stars 309 forks source link

Getting snapshot from IP camera #91

Open ozikus86 opened 4 years ago

ozikus86 commented 4 years ago

Hi. Im new to python. Im trying to write a script that connect to camera (dahua) and saves one frame on a local ftp - I succeded but i didint use ONFIV but i captured one frame using RSTP. However it would be very usefull for me to use onvif. Im encountering HTTP_GET request problem. I wrote a code that sendsi mi URI (snapshot link) - when i paste it the browser - i get authentication window - i dont know how to bypass it. Ive been to page 8 google results page (it is considered to be deep web :)) and nothing works. Im sending my code: the problem with authorization 401 i constant. Ive tried everything

rom onvif import ONVIFCamera from PIL import Image import glob, os from requests.auth import HTTPBasicAuth import requests import urllib2 from StringIO import StringIO from base64 import b64encode mycam = ONVIFCamera('192.168.1.108', 80, 'admin', 'admin123','/home/pi/Downloads/onvif-0.2.0/wsdl')

Get Hostname

resp = mycam.devicemgmt.GetHostname() print 'My camera`s hostname: ' + str(resp.Name)

Get system date and time

media=mycam.create_media_service() media_service=mycam.create_media_service() allProfiles=media.GetProfiles() mainProfile=media.GetProfile({'ProfileToken':allProfiles[0]._token}) snapshot=media.GetSnapshotUri({'ProfileToken':mainProfile._token}) uri=snapshot.Uri

s=requests.Session()

s.get(uri,headers={})

auth=HTTPBasicAuth('admin','admin123')

r=requests.get(uri,auth=auth)

s.headers.update({'user':'admin'})

s.headers.update({'password':'admin123'})

print r.status_code

output=requests.post(uri,auth=HTTPBasicAuth('admin','admin123'))

r=s.get(uri)

print output

print uri

userAndPass=b64encode(b"admin:admin123").decode("ascii")

headers={'Authorization':'Basic %s' % userAndPass}

response=requests.get(uri,headers=headers)

print response.status_code

print 'Link do Kamery: '+uri+' '

content=urllib2.urlopen(response).read()

im=Image.open(content)

im.Save('/home/pi/Desktop/test.jpg',"JPEG")

JoaoBaggio commented 4 years ago

hi there. Try this: import urllib

filename = "/home/pi/Downloads/picture.jpg" #this is where you wanna save de snapshot urllib.urlretrieve(uri, filename) #where the uri is the snapshot link

i hope it helps

ozikus86 commented 4 years ago

nope - MediaUri instance has no attribute 'strip'

ozikus86 commented 4 years ago

sory. i dit sth wrong and corrected it. but i still get" Bez tytułu

JoaoBaggio commented 4 years ago

Try to take a look at: https://stackoverflow.com/questions/40221579/http-error-401-unauthorized-using-urllib-request-urlopen

zoldaten commented 1 month ago
from requests.auth import HTTPDigestAuth

res=requests.get(url, auth=HTTPDigestAuth('admin', 'admin'))
    if res.status_code == 200:
        with open('a.jpg', "wb") as fp:
            fp.write(res.content)