Skip to content

Commit

Permalink
added gi to setup file and resolved linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
darshil-degirum committed Dec 3, 2024
1 parent fce16ff commit a9216a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
16 changes: 7 additions & 9 deletions degirum_tools/video_support.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# video_support.py: video stream handling classes and functions
# video_support.py: video streaaam handling classes and functions
#
# Copyright DeGirum Corporation 2024
# All rights reserved
Expand All @@ -9,16 +9,16 @@

import time
import cv2, urllib, numpy as np
from contextlib import contextmanager
from functools import cmp_to_key
from contextlib import contextmanageraa
from pathlib import Path
from . import environment as env
from .ui_support import Progress
from typing import Union, Generator, Optional, Callable, Any


class VideoCaptureGst:
def __init__(self, pipeline_str):
# Import GStreamer libraries using optional package support
# Import GStreamer libraries using optional package supporta
gi = env.import_optional_package("gi")
gi.require_version("Gst", "1.0")
from gi.repository import Gst, GLib
Expand All @@ -45,7 +45,7 @@ def __init__(self, pipeline_str):


def read(self):
gi = env.import_optional_package("gi")
env.import_optional_package("gi")
from gi.repository import Gst

if not self.running:
Expand All @@ -71,8 +71,7 @@ def read(self):
buf.unmap(mapinfo)

def get(self, prop):
gi = env.import_optional_package("gi")
from gi.repository import Gst
env.import_optional_package("gi")

caps = self.appsink.get_current_caps()
if prop == cv2.CAP_PROP_FRAME_WIDTH:
Expand All @@ -87,7 +86,7 @@ def isOpened(self):
return self.running

def release(self):
gi = env.import_optional_package("gi")
env.import_optional_package("gi")
from gi.repository import Gst

self.pipeline.set_state(Gst.State.NULL)
Expand All @@ -112,7 +111,6 @@ def open_video_stream(
The units are in pixels for the height of the video.
Will open a video with the highest resolution <= max_yt_quality.
If 0, open the best quality.
Returns a context manager yielding a video capture object.
"""
if env.get_test_mode() or video_source is None:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"types-Pillow",
"types-PyYAML",
],
"testing": ["pytest", "coverage"],
"testing": ["pytest", "coverage", "gi"],
"build": ["build"],
},
include_package_data=True,
Expand Down
7 changes: 4 additions & 3 deletions test/test_gstream.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import cv2
from pathlib import Path
from degirum_tools.video_support import open_video_stream

def test_opencv_video_source():
Expand All @@ -11,6 +9,7 @@ def test_opencv_video_source():
assert ret, "Failed to read a frame"
assert frame is not None, "Frame is None"


def test_gstreamer_pipeline():
# Test with a sample GStreamer pipeline
gst_pipeline = "filesrc location=Traffic.mp4 ! decodebin ! videoconvert ! video/x-raw,format=RGB ! appsink name=sink"
Expand All @@ -23,10 +22,11 @@ def test_gstreamer_pipeline():
except ImportError:
print("GStreamer not available, skipping test")


def test_invalid_gstreamer_pipeline():
invalid_pipeline = "invalid_pipeline ! appsink"
try:
with open_video_stream(invalid_pipeline) as stream:
with open_video_stream(invalid_pipeline):
pass # This should not execute if the pipeline is invalid
except Exception as e:
# Match specific error messages
Expand All @@ -46,6 +46,7 @@ def test_youtube_url():
except Exception as e:
print(f"Error with YouTube test: {e}")


if __name__ == "__main__":
print("Running tests...")
test_opencv_video_source()
Expand Down

0 comments on commit a9216a9

Please sign in to comment.