Skip to content

Commit

Permalink
added main_test
Browse files Browse the repository at this point in the history
  • Loading branch information
RBhupi committed Nov 22, 2024
1 parent a88b94b commit 1edf4dd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 11 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from inf import getInfoDict, cropMarginInfo, cropFrame
import cv2

# Configure logger
logging.basicConfig()
logger = logging.getLogger(__name__)

def upload_image(sky_curr, timestamp, thres_otsu, plugin):
img2_file_name = f"img2_{timestamp}.jpg"
Expand Down Expand Up @@ -118,28 +121,34 @@ def analyze_and_publish_segments(segments, mag, ang, plugin, sample, args):
def main(args):
inf = initialize_plugin(args)
fcount = 0

with Plugin() as plugin, Camera(args.input) as camera:
frame_time_curr, sky_curr, fcount = process_frame(camera, inf, fcount)

logger.info("Starting main loop")
while True:
logger.debug("Inside main loop")
logger.debug("Processing frame")
if inf["interval"] > 0:
time.sleep(inf["interval"])

with Camera(args.input) as camera:
frame_time_prev = frame_time_curr
frame_time_curr, sky_new, fcount = process_frame(camera, inf, fcount)

logger.debug("Computing optical flow")
vel_factor = 60 / (frame_time_curr - frame_time_prev)
sky_prev, sky_curr = sky_curr, sky_new

flow, mag, ang = compute_optical_flow(sky_prev, sky_curr, inf, vel_factor)
logger.debug("Thresholding and publishing")
should_upload, thres_otsu = threshold_and_publish(mag, plugin, camera.snapshot(), args.thr)

if should_upload:
upload_image(sky_curr, camera.snapshot().timestamp, thres_otsu, plugin)

logger.debug("Creating segments")
segments, _ = create_segments(sky_curr, mag, ang, args.segments)
logger.debug("Analyzing and publishing segments")
analyze_and_publish_segments(segments, mag, ang, plugin, camera.snapshot(), args)

if args.oneshot:
Expand All @@ -155,7 +164,7 @@ def main(args):
parser.add_argument("--q", type=int, default=2)
parser.add_argument("--thr", type=int, default=50)
parser.add_argument("--segments", type=int, default=100)
parser.add_argument("--seg_pub", type=int, default=3)
parser.add_argument("--seg_pub", type=int, default=9)
parser.add_argument("--oneshot", action="store_true")
args = parser.parse_args()
main(args)
19 changes: 18 additions & 1 deletion test_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest
from unittest.mock import MagicMock, patch
import logging
import io
import numpy as np
import cv2

Expand Down Expand Up @@ -108,6 +110,21 @@ def test_upload_image(self, mock_imwrite):
f"img2_{timestamp}.jpg", meta={"thres_otsu": "5"}, timestamp=timestamp
)


def setUp(self):
self.args = MagicMock()
self.args.input = "file://test-data/sgptsimovieS01.a1.20160726.000000.mpg"
self.args.i = 1 # for testing
self.args.c = 0 # Red channel
self.args.k = 0.9
self.args.q = 2
self.args.thr = 1000 # for testing
self.args.segments = 25
self.args.seg_pub = 3
self.args.oneshot = True

def test_main(self):
"""Test the main function with the actual input file."""
main(self.args)

if __name__ == "__main__":
unittest.main()

0 comments on commit 1edf4dd

Please sign in to comment.