Last active
January 23, 2023 07:14
-
-
Save alvonx/3c824592fc4dbfff3e8f644afdb284fc to your computer and use it in GitHub Desktop.
Save snapshot from ONVIF enabled camera using python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install onvif-zeep | |
# import package | |
import requests | |
from onvif import ONVIFCamera | |
# create onvif camera object | |
cam_obj = ONVIFCamera(CAMERA_HOST, CAMERA_PORT, CAMERA_USER, CAMERA_PASS) | |
# create media service | |
media_service = cam_obj.create_media_service() | |
# get all profiles available | |
get_all_profiles = media_service.GetProfiles() | |
# select the main profile (main stream) | |
main_stream = get_all_profiles[0] | |
# access profile token (some profile may contain token in '_token' key) | |
profile_token = main_stream.token | |
# get snapshot uri all info | |
snapshot_uri_info = media_service.GetSnapshotUri({ | |
'ProfileToken': profile_token | |
}) | |
# access the snap uri | |
snapshot_uri = str(snapshot_uri_info.Uri) | |
# if you want to add user and pass in url | |
snapshot_uri = snapshot_uri.replace('http://', f'http://{CAMERA_USER}:{CAMERA_PASS}@') | |
print(snapshot_uri) | |
# save snapshot to file | |
response = requests.request("POST", snapshot_uri) | |
if response.status_code == 200: | |
image_name = "somename" | |
fp = open(f'{image_name}.jpg', 'wb') | |
fp.write(response.content) | |
fp.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment