Created
October 25, 2024 10:56
-
-
Save gyillikci/aecceef56a9444cadb9ae041cf8f5e48 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from onvif import ONVIFCamera | |
import cv2 | |
import requests | |
import numpy as np | |
# Replace with your camera's details | |
CAMERA_HOST = '192.168.0.189' | |
CAMERA_PORT = 80 | |
CAMERA_USER = 'admin' | |
CAMERA_PASS = 'password' | |
def get_snapshot_uri(): | |
mycam = ONVIFCamera(CAMERA_HOST, CAMERA_PORT, CAMERA_USER, CAMERA_PASS) | |
media_service = mycam.create_media_service() | |
profiles = media_service.GetProfiles() | |
token = profiles[0].token | |
request = media_service.create_type('GetSnapshotUri') | |
request.ProfileToken = token | |
snapshot_uri = media_service.GetSnapshotUri(request).Uri | |
return snapshot_uri | |
def capture_frame(snapshot_uri): | |
response = requests.get(snapshot_uri, auth=(CAMERA_USER, CAMERA_PASS), stream=True) | |
if response.status_code == 200: | |
img_array = np.asarray(bytearray(response.content), dtype=np.uint8) | |
frame = cv2.imdecode(img_array, cv2.IMREAD_COLOR) | |
return frame | |
else: | |
print('Failed to get snapshot') | |
return None | |
def save_frame(frame, filename='snapshot.jpg'): | |
cv2.imwrite(filename, frame) | |
def main(): | |
snapshot_uri = get_snapshot_uri() | |
if snapshot_uri: | |
frame = capture_frame(snapshot_uri) | |
if frame is not None: | |
save_frame(frame) | |
print('Snapshot saved as snapshot.jpg') | |
else: | |
print('Failed to capture frame') | |
else: | |
print('Failed to get snapshot URI') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment