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

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +1 -81
handler.py CHANGED
@@ -1,12 +1,6 @@
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=""):
@@ -40,78 +34,4 @@ class EndpointHandler():
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):
60
- self.model_name = model_name
61
- self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
62
- self.model = AutoModelForObjectDetection.from_pretrained(self.model_name)
63
- self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
64
- self.model.to(self.device)
65
-
66
- def preprocess_image(self, image_path):
67
- # Load and preprocess the image
68
- image = Image.open(image_path)
69
- transform = transforms.Compose([
70
- transforms.Resize((224, 224)), # Resize the image to match the model's input size
71
- transforms.ToTensor(), # Convert the image to a PyTorch tensor
72
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Normalize the image
73
- ])
74
- image = transform(image).unsqueeze(0) # Add batch dimension to the image
75
- image = image.to(self.device)
76
- return image
77
-
78
- def predict_objects(self, image_path):
79
- image = self.preprocess_image(image_path)
80
-
81
- # Perform inference
82
- with torch.no_grad():
83
- outputs = self.model(image)
84
-
85
- # Process the outputs and extract relevant information
86
- # The specifics depend on the model you are using and its output format
87
- # Extract the predicted objects and their bounding boxes
88
-
89
- # Sample code to convert the outputs to a dictionary
90
- predictions = {
91
- "objects": ["object_1", "object_2", ...],
92
- "bounding_boxes": [[x_min, y_min, x_max, y_max], ...],
93
- }
94
-
95
- return predictions
96
-
97
- def __call__(self, event, context):
98
- if "image_path" not in event:
99
- return {
100
- "statusCode": 400,
101
- "body": json.dumps("Error: Please provide an 'image_path' parameter."),
102
- }
103
-
104
- image_path = event["image_path"]
105
-
106
- try:
107
- predictions = self.predict_objects(image_path)
108
- return {
109
- "statusCode": 200,
110
- "body": json.dumps(predictions),
111
- }
112
- except Exception as e:
113
- return {
114
- "statusCode": 500,
115
- "body": json.dumps(f"Error: {str(e)}"),
116
- }
117
- """
 
 
 
 
1
  from huggingface_hub import hf_hub_download
2
  from typing import Dict, List, Any
3
  from ultralytics import YOLO
 
 
 
4
 
5
  class EndpointHandler():
6
  def __init__(self, path=""):
 
34
  return {
35
  "statusCode": 500,
36
  "body": json.dumps(f"Error: {str(e)}"),
37
+ }