Navy commited on
Commit
1b6f8a9
·
1 Parent(s): 026fdce

feat: adding code response

Browse files
Files changed (2) hide show
  1. app.py +2 -0
  2. core/defect_detection.py +9 -2
app.py CHANGED
@@ -88,6 +88,7 @@ async def start_detection(data: Dict):
88
  for err in validation_errors:
89
  logger.error(f" - {err}")
90
  return {
 
91
  "status": "error",
92
  "station_id": station_id,
93
  "camera_count": len(cameras),
@@ -112,6 +113,7 @@ async def start_detection(data: Dict):
112
  asyncio.create_task(run_detection_group(station_id, cameras, webhook_url, model, parts))
113
 
114
  return {
 
115
  "status": "started",
116
  "station_id": station_id,
117
  "camera_count": len(cameras),
 
88
  for err in validation_errors:
89
  logger.error(f" - {err}")
90
  return {
91
+ "code": 400,
92
  "status": "error",
93
  "station_id": station_id,
94
  "camera_count": len(cameras),
 
113
  asyncio.create_task(run_detection_group(station_id, cameras, webhook_url, model, parts))
114
 
115
  return {
116
+ "code": 200,
117
  "status": "started",
118
  "station_id": station_id,
119
  "camera_count": len(cameras),
core/defect_detection.py CHANGED
@@ -10,9 +10,9 @@ load_dotenv()
10
  MODEL_VERSION = os.getenv("MODEL_VERSION","v1.0.0")
11
  WEBHOOK_URL = os.getenv("WEBHOOK_URL")
12
 
13
- MAX_RUNTIME_SEC = float(os.getenv("MAX_RUNTIME_SEC", "20"))
14
  FRAME_FAIL_SLEEP = float(os.getenv("FRAME_FAIL_SLEEP", "0.05"))
15
- DEFAULT_FPS = float(os.getenv("DEFAULT_FPS", "25"))
16
  WEBHOOK_TIMEOUT = float(os.getenv("WEBHOOK_TIMEOUT", "10.0"))
17
 
18
  # ============================================================
@@ -31,6 +31,7 @@ def detect_defect_from_video_url(station_id, camera_id: str, video_url: str, mod
31
  if not cap.isOpened():
32
  logger.error(f"[ERROR] Cannot open video URL: {video_url}")
33
  return {
 
34
  "station_id": station_id,
35
  "camera_id": camera_id,
36
  "status": "error",
@@ -110,6 +111,7 @@ def detect_defect_from_video_url(station_id, camera_id: str, video_url: str, mod
110
  logger.info(f"[DETECTED] Camera {camera_id} → {defect_name} ({conf:.2f})")
111
 
112
  return {
 
113
  "station_id": station_id,
114
  "camera_id": camera_id,
115
  "status": "success",
@@ -143,6 +145,7 @@ def detect_defect_from_video_url(station_id, camera_id: str, video_url: str, mod
143
  filepath = None
144
 
145
  return {
 
146
  "station_id": station_id,
147
  "camera_id": camera_id,
148
  "status": "success",
@@ -189,15 +192,19 @@ async def run_detection_group(station_id: str, cameras: List[Dict], webhook_url:
189
 
190
  if all_error:
191
  status = "error"
 
192
  message = "All cameras failed during detection"
193
  elif has_error:
194
  status = "partial_error"
 
195
  message = "Some cameras failed during detection"
196
  else:
197
  status = "success"
 
198
  message = "Success detecting defects"
199
 
200
  payload = {
 
201
  "status": status,
202
  "timestamp": time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime()),
203
  "model_version": MODEL_VERSION,
 
10
  MODEL_VERSION = os.getenv("MODEL_VERSION","v1.0.0")
11
  WEBHOOK_URL = os.getenv("WEBHOOK_URL")
12
 
13
+ MAX_RUNTIME_SEC = float(os.getenv("MAX_RUNTIME_SEC", "5"))
14
  FRAME_FAIL_SLEEP = float(os.getenv("FRAME_FAIL_SLEEP", "0.05"))
15
+ DEFAULT_FPS = float(os.getenv("DEFAULT_FPS", "10"))
16
  WEBHOOK_TIMEOUT = float(os.getenv("WEBHOOK_TIMEOUT", "10.0"))
17
 
18
  # ============================================================
 
31
  if not cap.isOpened():
32
  logger.error(f"[ERROR] Cannot open video URL: {video_url}")
33
  return {
34
+ "code": 400,
35
  "station_id": station_id,
36
  "camera_id": camera_id,
37
  "status": "error",
 
111
  logger.info(f"[DETECTED] Camera {camera_id} → {defect_name} ({conf:.2f})")
112
 
113
  return {
114
+ "code": 200,
115
  "station_id": station_id,
116
  "camera_id": camera_id,
117
  "status": "success",
 
145
  filepath = None
146
 
147
  return {
148
+ "code": 200,
149
  "station_id": station_id,
150
  "camera_id": camera_id,
151
  "status": "success",
 
192
 
193
  if all_error:
194
  status = "error"
195
+ code = 400
196
  message = "All cameras failed during detection"
197
  elif has_error:
198
  status = "partial_error"
199
+ code = 200
200
  message = "Some cameras failed during detection"
201
  else:
202
  status = "success"
203
+ code = 200
204
  message = "Success detecting defects"
205
 
206
  payload = {
207
+ "code": code,
208
  "status": status,
209
  "timestamp": time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime()),
210
  "model_version": MODEL_VERSION,