JohnJoelMota commited on
Commit
d650227
·
verified ·
1 Parent(s): aa53b45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -3
app.py CHANGED
@@ -43,7 +43,6 @@ def recommend_model(image):
43
  img_array = np.array(image)
44
  height, width = img_array.shape[:2]
45
  pixel_variance = np.var(img_array)
46
- # Basic heuristic: DETR is better for high-resolution, complex images; Faster R-CNN for smaller, simpler ones
47
  if height * width > 1000 * 1000 or pixel_variance > 1000:
48
  return "DETR is recommended for high-resolution or complex images."
49
  else:
@@ -67,8 +66,11 @@ def detect_objects_frcnn(image, threshold=0.5):
67
 
68
  try:
69
  threshold = float(threshold) if threshold is not None else 0.5
 
 
 
70
  transform = FasterRCNN_ResNet50_FPN_Weights.DEFAULT.transforms()
71
- image_tensor = transform(image).unsqueeze(0)
72
 
73
  with torch.no_grad():
74
  prediction = frcnn_model(image_tensor)[0]
@@ -123,7 +125,9 @@ def detect_objects_detr(image, threshold=0.9):
123
  return Image.open(buf)
124
 
125
  try:
126
- inputs = detr_processor(images=image, return_tensors="pt")
 
 
127
  outputs = detr_model(**inputs)
128
  target_sizes = torch.tensor([image.size[::-1]])
129
  results = detr_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=threshold)[0]
 
43
  img_array = np.array(image)
44
  height, width = img_array.shape[:2]
45
  pixel_variance = np.var(img_array)
 
46
  if height * width > 1000 * 1000 or pixel_variance > 1000:
47
  return "DETR is recommended for high-resolution or complex images."
48
  else:
 
66
 
67
  try:
68
  threshold = float(threshold) if threshold is not None else 0.5
69
+ # Convert image to RGB and ensure float32 for transform
70
+ image = image.convert('RGB')
71
+ img_array = np.array(image).astype(np.float32) / 255.0
72
  transform = FasterRCNN_ResNet50_FPN_Weights.DEFAULT.transforms()
73
+ image_tensor = transform(Image.fromarray((img_array * 255).astype(np.uint8))).unsqueeze(0)
74
 
75
  with torch.no_grad():
76
  prediction = frcnn_model(image_tensor)[0]
 
125
  return Image.open(buf)
126
 
127
  try:
128
+ # Convert image to RGB and process with padding
129
+ image = image.convert('RGB')
130
+ inputs = detr_processor(images=image, return_tensors="pt", padding=True)
131
  outputs = detr_model(**inputs)
132
  target_sizes = torch.tensor([image.size[::-1]])
133
  results = detr_processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=threshold)[0]