syedfaisalabrar commited on
Commit
d885a52
·
verified ·
1 Parent(s): 04dd650

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import torch
3
  import cv2
4
  import numpy as np
5
- from PIL import Image
6
  from ultralytics import YOLO
7
  import json
8
 
@@ -11,20 +11,17 @@ model_path = "best.pt"
11
  model = YOLO(model_path)
12
 
13
  def preprocess_image(image):
14
- """Apply enhancement filters and resize image before detection."""
15
- image = np.array(image)
16
 
17
-
18
- image = cv2.convertScaleAbs(image, alpha=0.8, beta=0) # Brightness reduction
19
- image = cv2.GaussianBlur(image, (3, 3), 0) # Denoising
20
- kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) # Sharpening
21
- image = cv2.filter2D(image, -1, kernel)
22
 
23
-
24
- height, width = image.shape[:2]
25
- new_width = 800
26
- new_height = int((new_width / width) * height)
27
- image = cv2.resize(image, (new_width, new_height))
28
 
29
  return image
30
 
 
2
  import torch
3
  import cv2
4
  import numpy as np
5
+ from PIL import Image, ImageEnhance
6
  from ultralytics import YOLO
7
  import json
8
 
 
11
  model = YOLO(model_path)
12
 
13
  def preprocess_image(image):
14
+ image = Image.fromarray(np.array(image))
 
15
 
16
+ image = ImageEnhance.Sharpness(image).enhance(2.0) # Increase sharpness
17
+ image = ImageEnhance.Contrast(image).enhance(1.5) # Increase contrast
18
+ image = ImageEnhance.Brightness(image).enhance(0.8) # Reduce brightness
 
 
19
 
20
+ # Resize image to 800px width while maintaining aspect ratio
21
+ width = 800
22
+ aspect_ratio = image.height / image.width
23
+ height = int(width * aspect_ratio)
24
+ image = image.resize((width, height))
25
 
26
  return image
27