janasumit2911 commited on
Commit
14a261b
·
verified ·
1 Parent(s): 9ada592

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -20
app.py CHANGED
@@ -39,36 +39,46 @@ def create_solutions(image_urls, all_bboxes, all_bboxes2):
39
  img_id +=1
40
  return solutions
41
 
42
- def send_results_to_api(data, result_url):
43
- # Example function to send results to an API
44
- headers = {"Content-Type": "application/json"}
45
- response = requests.post(result_url, json=data, headers=headers)
46
- if response.status_code == 200:
47
- return response.json() # Return any response from the API if needed
48
- else:
49
- return {"error": f"Failed to send results to API: {response.status_code}"}
50
 
51
  def process_images(params):
52
- # Parse the JSON string into a dictionary
53
- params = json.loads(params)
 
 
 
54
 
55
- image_urls = params.get("image_urls", [])
56
- api = params.get("api", "")
57
- job_id = params.get("job_id", "")
58
-
59
- images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
60
-
 
 
 
 
 
 
 
61
  all_bboxes, all_bboxes2 = detect_objects(images) # Perform object detection
62
  solutions = create_solutions(image_urls, all_bboxes, all_bboxes2) # Create solutions with image URLs and bounding boxes
63
 
64
- result_url = f"{api}/{job_id}"
65
- send_results_to_api(solutions, result_url)
66
 
67
- return json.dumps({"solutions": solutions}, indent=4)
68
 
69
 
70
  inputt = gr.Textbox(label="Parameters (JSON format)")
71
  outputs = gr.JSON()
72
 
73
  application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Detection with API Integration")
74
- application.launch(share=True)
 
39
  img_id +=1
40
  return solutions
41
 
42
+ # def send_results_to_api(data, result_url):
43
+ # # Example function to send results to an API
44
+ # headers = {"Content-Type": "application/json"}
45
+ # response = requests.post(result_url, json=data, headers=headers)
46
+ # if response.status_code == 200:
47
+ # return response.json() # Return any response from the API if needed
48
+ # else:
49
+ # return {"error": f"Failed to send results to API: {response.status_code}"}
50
 
51
  def process_images(params):
52
+ try:
53
+ params = json.loads(params)
54
+ except json.JSONDecodeError as e:
55
+ logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
56
+ return {"error":f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
57
 
58
+ image_urls = params.get("urls", [])
59
+ # api = params.get("api", "")
60
+ # job_id = params.get("job_id", "")
61
+
62
+ if not image_urls:
63
+ logging.error("Missing required parameters: 'urls'")
64
+ return {"error": "Missing required parameters: 'urls'"}
65
+ try:
66
+ images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
67
+ except Exception as e:
68
+ logging.error(f"Error loading images: {e}")
69
+ return {"error": f"Error loading images: {str(e)}"}
70
+
71
  all_bboxes, all_bboxes2 = detect_objects(images) # Perform object detection
72
  solutions = create_solutions(image_urls, all_bboxes, all_bboxes2) # Create solutions with image URLs and bounding boxes
73
 
74
+ # result_url = f"{api}/{job_id}"
75
+ # send_results_to_api(solutions, result_url)
76
 
77
+ return json.dumps({"solutions": solutions})
78
 
79
 
80
  inputt = gr.Textbox(label="Parameters (JSON format)")
81
  outputs = gr.JSON()
82
 
83
  application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Multiple Object Detection with API Integration")
84
+ application.launch()