janasumit2911 commited on
Commit
cbcf9cf
·
verified ·
1 Parent(s): 6a4cc2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -20
app.py CHANGED
@@ -3,6 +3,9 @@ from PIL import Image
3
  from ultralytics import YOLO
4
  import requests
5
  import json
 
 
 
6
 
7
  model = YOLO("Covid_Positive_Negative_Classify_v1.pt")
8
 
@@ -24,39 +27,50 @@ def create_solutions(image_urls, names, probss):
24
  solutions = [] #list to store all the objects
25
 
26
  for image_url, class_name, prob in zip(image_urls, names, probss):
27
- obj = {"image": image_url, "answer": [class_name] }
28
  solutions.append(obj)
29
  return solutions
30
 
31
- def send_results_to_api(data, result_url):
32
- # Example function to send results to an API
33
- headers = {"Content-Type": "application/json"}
34
- response = requests.post(result_url, json=data, headers=headers)
35
- if response.status_code == 200:
36
- return response.json() # Return any response from the API if needed
37
- else:
38
- return {"error": f"Failed to send results to API: {response.status_code}"}
39
 
40
  def process_images(params):
41
- # Parse the JSON string into a dictionary
42
- params = json.loads(params)
 
 
 
43
 
44
- image_urls = params.get("image_urls", [])
45
- api = params.get("api", "")
46
- job_id = params.get("job_id", "")
47
 
48
- images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
 
 
49
 
 
 
 
 
 
 
50
  names, probss = detect_objects(images) # Perform object detection
51
  solutions = create_solutions(image_urls, names, probss) # Create solutions with image URLs and bounding boxes
52
 
53
- result_url = f"{api}/{job_id}"
54
- send_results_to_api(solutions, result_url)
55
 
56
- return json.dumps({"solutions": solutions}, indent=4)
57
 
58
- inputt = gr.Textbox(label="Parameters (JSON format) Eg. img_url:['',''], api:'', job_id:'' ")
59
  outputs = gr.JSON()
60
 
61
  application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Covid +ve -ve Classification with API Integration")
62
- application.launch(share=True)
 
3
  from ultralytics import YOLO
4
  import requests
5
  import json
6
+ import logging
7
+
8
+ logging.basicConfig(level=logging.INFO)
9
 
10
  model = YOLO("Covid_Positive_Negative_Classify_v1.pt")
11
 
 
27
  solutions = [] #list to store all the objects
28
 
29
  for image_url, class_name, prob in zip(image_urls, names, probss):
30
+ obj = {"url": image_url, "answer": [class_name] }
31
  solutions.append(obj)
32
  return solutions
33
 
34
+ # def send_results_to_api(data, result_url):
35
+ # # Example function to send results to an API
36
+ # headers = {"Content-Type": "application/json"}
37
+ # response = requests.post(result_url, json=data, headers=headers)
38
+ # if response.status_code == 200:
39
+ # return response.json() # Return any response from the API if needed
40
+ # else:
41
+ # return {"error": f"Failed to send results to API: {response.status_code}"}
42
 
43
  def process_images(params):
44
+ try:
45
+ params = json.loads(params)
46
+ except json.JSONDecodeError as e:
47
+ logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
48
+ return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
49
 
50
+ image_urls = params.get("urls", [])
51
+ # api = params.get("api", "")
52
+ # job_id = params.get("job_id", "")
53
 
54
+ if not image_urls:
55
+ logging.error("Missing required parameters: 'urls'")
56
+ return {"error": "Missing required parameters: 'urls'"}
57
 
58
+ try:
59
+ images = [Image.open(requests.get(url, stream=True).raw) for url in image_urls] # images from URLs
60
+ except Exception as e:
61
+ logging.error(f"Error loading images: {e}")
62
+ return {"error": f"Error loading images: {str(e)}"}
63
+
64
  names, probss = detect_objects(images) # Perform object detection
65
  solutions = create_solutions(image_urls, names, probss) # Create solutions with image URLs and bounding boxes
66
 
67
+ # result_url = f"{api}/{job_id}"
68
+ # send_results_to_api(solutions, result_url)
69
 
70
+ return json.dumps({"solutions": solutions})
71
 
72
+ inputt = gr.Textbox(label="Parameters (JSON format) Eg. img_url:['','']")
73
  outputs = gr.JSON()
74
 
75
  application = gr.Interface(fn=process_images, inputs=inputt, outputs=outputs, title="Covid +ve -ve Classification with API Integration")
76
+ application.launch()