Drazcat-AI commited on
Commit
1a0508e
·
1 Parent(s): 1020314

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +39 -8
handler.py CHANGED
@@ -1,28 +1,59 @@
1
- from subprocess import run
2
  #run("pip install ultralytics", shell=True, check=True)
3
  #run("pip install opencv-python", shell=True, check=True)
4
  from huggingface_hub import hf_hub_download
5
  from typing import Dict, List, Any
6
  from ultralytics import YOLO
7
- import cv2
8
- import torch
9
- import numpy as np
 
10
  class EndpointHandler():
11
  def __init__(self, path=""):
12
  hf_hub_download(repo_id="Drazcat-AI/galletas", filename="yolov8_galletas/runs/detect/train/weights/best.pt")
13
- #self.model = YOLO(hf_hub_download(repo_id="Drazcat-AI/galletas", filename="yolov8_galletas/runs/detect/train/weights/best.pt", local_files_only=True))
 
 
 
 
 
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
16
 
17
- #model = YOLO('/root/.cache/huggingface/hub/models--Drazcat-AI--galletas/snapshots/d960a6784ad838e30f01690ce813fb4dc7460bc0/yolov8_galletas/runs/detect/train/weights/best.pt')
18
- model = YOLO(hf_hub_download(repo_id="Drazcat-AI/galletas", filename="yolov8_galletas/runs/detect/train/weights/best.pt", local_files_only=True))
19
- results = model('https://rocketpin-ml-images.s3.amazonaws.com/smu/visits/1120/IMG_20230609_173208_902.jpg', imgsz=800)
20
  img = cv2.imread('IMG_20230609_173208_902.jpg')
21
  img = cv2.resize(img, (640, 800))
22
 
23
  for result in results:
24
  cv2.imshow("result", result)
25
  cv2.waitKey(0)
 
26
  """
27
  class ObjectDetectionHandler:
28
  def __init__(self, model_name):
 
1
+ #from subprocess import run
2
  #run("pip install ultralytics", shell=True, check=True)
3
  #run("pip install opencv-python", shell=True, check=True)
4
  from huggingface_hub import hf_hub_download
5
  from typing import Dict, List, Any
6
  from ultralytics import YOLO
7
+ #import cv2
8
+ #import torch
9
+ #import numpy as np
10
+
11
  class EndpointHandler():
12
  def __init__(self, path=""):
13
  hf_hub_download(repo_id="Drazcat-AI/galletas", filename="yolov8_galletas/runs/detect/train/weights/best.pt")
14
+ self.model = YOLO(hf_hub_download(repo_id="Drazcat-AI/galletas", filename="yolov8_galletas/runs/detect/train/weights/best.pt", local_files_only=True))
15
+
16
+ def predict_objects(self, image_path):
17
+ results = self.model(image_path, imgsz=800)
18
+
19
+ for result in results:
20
+ print(result)
21
 
22
+ return result
23
+
24
+ def __call__(self, event, context):
25
+ if "image_path" not in event:
26
+ return {
27
+ "statusCode": 400,
28
+ "body": json.dumps("Error: Please provide an 'image_path' parameter."),
29
+ }
30
+
31
+ image_path = event["image_path"]
32
+
33
+ try:
34
+ predictions = self.predict_objects(image_path)
35
+ return {
36
+ "statusCode": 200,
37
+ "body": json.dumps(predictions),
38
+ }
39
+ except Exception as e:
40
+ return {
41
+ "statusCode": 500,
42
+ "body": json.dumps(f"Error: {str(e)}"),
43
+ }
44
+
45
+ """
46
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
47
 
48
+ #model = YOLO(hf_hub_download(repo_id="Drazcat-AI/galletas", filename="yolov8_galletas/runs/detect/train/weights/best.pt", local_files_only=True))
49
+ results = self.model('https://rocketpin-ml-images.s3.amazonaws.com/smu/visits/1120/IMG_20230609_173208_902.jpg', imgsz=800)
 
50
  img = cv2.imread('IMG_20230609_173208_902.jpg')
51
  img = cv2.resize(img, (640, 800))
52
 
53
  for result in results:
54
  cv2.imshow("result", result)
55
  cv2.waitKey(0)
56
+ """
57
  """
58
  class ObjectDetectionHandler:
59
  def __init__(self, model_name):