-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
125 lines (105 loc) · 4.73 KB
/
main.py
File metadata and controls
125 lines (105 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Main entry point for Medvision.
This script allows the user to process images, videos, perform retrieval, or launch the interactive dashboard.
"""
import argparse
import os
import sys
import cv2
import numpy as np
from config import DATASET_DIR, VIDEO_DIR, OUTPUT_DIR
from modules import image_processing, video_processing, optical_flow, retrieval, object_detection, dashboard
def process_image(input_path):
print(f"[INFO] Loading image from {input_path}")
image = image_processing.load_image(input_path)
if image is None:
print("[ERROR] Unable to load image!")
sys.exit(1)
# Preprocess image
processed = image_processing.preprocess_image(image)
# Save the processed image
output_path = os.path.join(OUTPUT_DIR, "processed_image.png")
image_processing.save_image(output_path, processed)
print(f"[INFO] Processed image saved at {output_path}")
# Run object detection
detections = object_detection.detect_objects(processed)
image_with_boxes = object_detection.draw_detections(processed, detections)
output_detection = os.path.join(OUTPUT_DIR, "detected_objects.png")
image_processing.save_image(output_detection, image_with_boxes)
print(f"[INFO] Object detection result saved at {output_detection}")
def process_video(input_path):
print(f"[INFO] Processing video from {input_path}")
cap = video_processing.load_video(input_path)
if not cap:
print("[ERROR] Unable to open video!")
sys.exit(1)
processed_frames = []
prev_frame = None
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
frame_count += 1
gray = image_processing.preprocess_image(frame, grayscale=True)
if prev_frame is not None:
# Compute optical flow between frames
flow_fb = optical_flow.compute_farneback_flow(prev_frame, gray)
flow_lk = optical_flow.compute_lucas_kanade_flow(prev_frame, gray)
flow_dl = optical_flow.compute_deep_flow(prev_frame, gray)
# Overlay flow visualization on frame
frame = optical_flow.draw_flow(frame, flow_fb, color=(0, 255, 0))
frame = optical_flow.draw_flow(frame, flow_lk, color=(255, 0, 0))
frame = optical_flow.draw_flow(frame, flow_dl, color=(0, 0, 255))
processed_frames.append(frame)
prev_frame = gray
output_video = os.path.join(OUTPUT_DIR, "processed_video.avi")
video_processing.write_video(output_video, processed_frames)
print(f"[INFO] Processed video saved at {output_video}")
def perform_retrieval(query_path, dataset_path):
print(f"[INFO] Performing image retrieval for query: {query_path}")
query_image = image_processing.load_image(query_path)
if query_image is None:
print("[ERROR] Unable to load query image!")
sys.exit(1)
# Initialize retrieval engine
retrieval_engine = retrieval.ImageRetrieval(dataset_path)
retrieval_engine.build_index() # Build or load index
# Perform search
similar_images = retrieval_engine.search(query_image)
print("[INFO] Retrieval results:")
for idx, sim in enumerate(similar_images):
print(f"Result {idx+1}: {sim}")
def launch_dashboard():
print("[INFO] Launching interactive dashboard...")
dashboard.run_dashboard()
def main():
parser = argparse.ArgumentParser(description="Medvision - Advanced Medical Image/Video Processing Suite")
parser.add_argument("--mode", required=True, choices=["process_image", "process_video", "retrieval", "dashboard"],
help="Mode to run the application.")
parser.add_argument("--input", type=str, help="Path to the input image or video file.")
parser.add_argument("--query", type=str, help="Path to the query image for retrieval.")
parser.add_argument("--dataset", type=str, default=DATASET_DIR, help="Path to the dataset for image retrieval.")
args = parser.parse_args()
if args.mode == "process_image":
if not args.input:
print("[ERROR] Please specify --input for image processing")
sys.exit(1)
process_image(args.input)
elif args.mode == "process_video":
if not args.input:
print("[ERROR] Please specify --input for video processing")
sys.exit(1)
process_video(args.input)
elif args.mode == "retrieval":
if not args.query:
print("[ERROR] Please specify --query for image retrieval")
sys.exit(1)
perform_retrieval(args.query, args.dataset)
elif args.mode == "dashboard":
launch_dashboard()
else:
print("[ERROR] Invalid mode selected.")
sys.exit(1)
if __name__ == "__main__":
main()