DurgaDeepak commited on
Commit
86f4d8f
·
verified ·
1 Parent(s): aec7cfd

Update core/process.py

Browse files
Files changed (1) hide show
  1. core/process.py +119 -0
core/process.py CHANGED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import time
3
+ import timeout_decorator
4
+
5
+ @timeout_decorator.timeout(35, use_signals=False) # 35 sec limit per image
6
+ def process_image(
7
+ image: Image.Image,
8
+ run_det: bool,
9
+ det_model: str,
10
+ det_confidence: float,
11
+ run_seg: bool,
12
+ seg_model: str,
13
+ run_depth: bool,
14
+ depth_model: str,
15
+ blend: float
16
+ ):
17
+ """
18
+ Runs selected perception tasks on the input image and packages results.
19
+ Args:
20
+ image (PIL.Image): Input image.
21
+ run_det (bool): Run object detection.
22
+ det_model (str): Detection model key.
23
+ det_confidence (float): Detection confidence threshold.
24
+ run_seg (bool): Run segmentation.
25
+ seg_model (str): Segmentation model key.
26
+ run_depth (bool): Run depth estimation.
27
+ depth_model (str): Depth model key.
28
+ blend (float): Overlay blend alpha (0.0 - 1.0).
29
+ Returns:
30
+ Tuple[Image, dict, Tuple[str, bytes]]: Final image, scene JSON, and downloadable ZIP.
31
+ """
32
+ logger.info("Starting image processing pipeline.")
33
+ start_time = time.time()
34
+ outputs, scene = {}, {}
35
+ combined_np = np.array(image)
36
+
37
+ try:
38
+ # Detection
39
+ if run_det:
40
+ logger.info(f"Running detection with model: {det_model}")
41
+ load_start = time.time()
42
+ model = get_model("detection", DETECTION_MODEL_MAP[det_model], device="cpu")
43
+ logger.info(f"{det_model} detection model loaded in {time.time() - load_start:.2f} seconds.")
44
+ boxes = model.predict(image, conf_threshold=det_confidence)
45
+ overlay = model.draw(image, boxes)
46
+ combined_np = np.array(overlay)
47
+ buf = io.BytesIO()
48
+ overlay.save(buf, format="PNG")
49
+ outputs["detection.png"] = buf.getvalue()
50
+ scene["detection"] = boxes
51
+
52
+ # Segmentation
53
+ if run_seg:
54
+ logger.info(f"Running segmentation with model: {seg_model}")
55
+ load_start = time.time()
56
+ model = get_model("segmentation", SEGMENTATION_MODEL_MAP[seg_model], device="cpu")
57
+ logger.info(f"{seg_model} segmentation model loaded in {time.time() - load_start:.2f} seconds.")
58
+ mask = model.predict(image)
59
+ overlay = model.draw(image, mask, alpha=blend)
60
+ combined_np = cv2.addWeighted(combined_np, 1 - blend, np.array(overlay), blend, 0)
61
+ buf = io.BytesIO()
62
+ overlay.save(buf, format="PNG")
63
+ outputs["segmentation.png"] = buf.getvalue()
64
+ scene["segmentation"] = mask.tolist()
65
+
66
+ # Depth Estimation
67
+ if run_depth:
68
+ logger.info(f"Running depth estimation with model: {depth_model}")
69
+ load_start = time.time()
70
+ model = get_model("depth", DEPTH_MODEL_MAP[depth_model], device="cpu")
71
+ logger.info(f"{depth_model} depth model loaded in {time.time() - load_start:.2f} seconds.")
72
+ dmap = model.predict(image)
73
+ norm_dmap = ((dmap - dmap.min()) / (dmap.ptp()) * 255).astype(np.uint8)
74
+ d_pil = Image.fromarray(norm_dmap)
75
+ combined_np = cv2.addWeighted(combined_np, 1 - blend, np.array(d_pil.convert("RGB")), blend, 0)
76
+ buf = io.BytesIO()
77
+ d_pil.save(buf, format="PNG")
78
+ outputs["depth_map.png"] = buf.getvalue()
79
+ scene["depth"] = dmap.tolist()
80
+
81
+ # Final image overlay
82
+ final_img = Image.fromarray(combined_np)
83
+ buf = io.BytesIO()
84
+ final_img.save(buf, format="PNG")
85
+ outputs["scene_blueprint.png"] = buf.getvalue()
86
+
87
+ # Scene description
88
+ try:
89
+ scene_json = describe_scene(**scene)
90
+ except Exception as e:
91
+ logger.warning(f"describe_scene failed: {e}")
92
+ scene_json = {"error": str(e)}
93
+ telemetry = {
94
+ "session_id": generate_session_id(),
95
+ "runtime_sec": round(log_runtime(start_time), 2),
96
+ "used_models": {
97
+ "detection": det_model if run_det else None,
98
+ "segmentation": seg_model if run_seg else None,
99
+ "depth": depth_model if run_depth else None
100
+ }
101
+ }
102
+ scene_json["telemetry"] = telemetry
103
+
104
+ outputs["scene_description.json"] = json.dumps(scene_json, indent=2).encode("utf-8")
105
+
106
+ # ZIP file creation
107
+ zip_buf = io.BytesIO()
108
+ with zipfile.ZipFile(zip_buf, "w") as zipf:
109
+ for name, data in outputs.items():
110
+ zipf.writestr(name, data)
111
+
112
+ elapsed = log_runtime(start_time)
113
+ logger.info(f"Image processing completed in {elapsed:.2f} seconds.")
114
+
115
+ return final_img, scene_json, ("uvis_results.zip", zip_buf.getvalue())
116
+
117
+ except Exception as e:
118
+ logger.error(f"Error in processing pipeline: {e}")
119
+ return None, {"error": str(e)}, None