kdb8756 commited on
Commit
76ac45d
·
1 Parent(s): 2aa3a95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -19
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import cv2
 
 
2
  import numpy as np
3
  import gradio as gr
4
- from PIL import ImageOps
5
  import warnings
6
 
7
  # Filter out user warnings to avoid any unnecessary clutter in the output
@@ -9,8 +10,11 @@ warnings.filterwarnings("ignore", category=UserWarning)
9
 
10
  font = cv2.FONT_HERSHEY_SIMPLEX
11
  font_scale = 1
 
 
 
12
 
13
- def search_contours(mask, frame):
14
  contours_count = 0
15
  contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
16
  pip_count = 0
@@ -30,6 +34,18 @@ def search_contours(mask, frame):
30
  cv2.putText(frame, str(pip_count), (cX - 16, cY + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
31
  return pip_count, frame
32
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  sample_images = {
34
  "23.jpg": "23.jpg",
35
  "28.png": "28.png",
@@ -43,12 +59,8 @@ def detect_pips(uploaded_image, sample_image_selection, hue_threshold):
43
  if uploaded_image is not None:
44
  image = uploaded_image[:, :, :3]
45
  else:
46
- image = cv2.imread(sample_images[sample_image_selection])
47
-
48
- # If only sample_image_selection is provided, return the sample image without processing
49
- if uploaded_image is None and hue_threshold == 250:
50
- return image, image
51
-
52
  image = cv2.resize(image, (512, 512))
53
  hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
54
  hue = 60
@@ -56,22 +68,18 @@ def detect_pips(uploaded_image, sample_image_selection, hue_threshold):
56
  lower_hsv = np.array([lower_hue, 50, 20])
57
  upper_hsv = np.array([upper_hue, 255, 255])
58
  mask = cv2.inRange(hsv, lower_hsv, upper_hsv)
59
- count, result_frame = search_contours(mask, image)
60
  cv2.putText(result_frame, f'Total pip count is: {count}', (10, result_frame.shape[0] - 30), font, 1, (0, 255, 255), 2, cv2.LINE_AA)
61
-
62
- return result_frame, image
63
 
64
  iface = gr.Interface(
65
  detect_pips,
66
  inputs=[
67
- gr.inputs.Image(type="numpy", label="Upload Image", source="upload", optional=True), # Image upload
68
- gr.inputs.Dropdown(choices=list(sample_images.keys()), label="Or Select a Sample Image", default=None), # Dropdown for sample images
69
  gr.inputs.Slider(label="Hue Threshold", minimum=0, maximum=500, step=1, default=250)
70
  ],
71
- outputs=[
72
- gr.outputs.Image(type="numpy", label="Processed Result"), # Processed image output
73
- gr.outputs.Image(type="numpy", label="Original/Selected Image") # Original or selected image output
74
- ],
75
  title='<b><font size="4">🁫Image Processing demonstration using OpenCV and Python:🁫</b>',
76
  description='<div style="text-align:center;"><b style="font-size:20px;">App to help you keep score when playing Dominos</b><br>Use your iPad to take a photo and upload to calculate a score<br>Note: lighting and camera angles are key factors :)</div>',
77
  allow_flagging=False,
@@ -79,5 +87,6 @@ iface = gr.Interface(
79
  theme="dark",
80
  )
81
 
82
- iface.launch()
83
-
 
 
1
  import cv2
2
+ from matplotlib.pyplot import hsv
3
+ from PIL import ImageOps
4
  import numpy as np
5
  import gradio as gr
 
6
  import warnings
7
 
8
  # Filter out user warnings to avoid any unnecessary clutter in the output
 
10
 
11
  font = cv2.FONT_HERSHEY_SIMPLEX
12
  font_scale = 1
13
+ color_search = np.zeros((200, 200, 3), np.uint8)
14
+ color_selected = np.zeros((200, 200, 3), np.uint8)
15
+ hue = 0
16
 
17
+ def search_contours(mask, frame, source):
18
  contours_count = 0
19
  contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
20
  pip_count = 0
 
34
  cv2.putText(frame, str(pip_count), (cX - 16, cY + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
35
  return pip_count, frame
36
 
37
+ def center_crop_with_padding(image):
38
+ im = Image.fromarray(image)
39
+ width, height = im.size
40
+ new_size = min(width, height)
41
+ im = im.crop(((width - new_size) // 2, (height - new_size) // 2, (width + new_size) // 2, (height + new_size) // 2))
42
+ im = im.crop((25, 25, new_size - 25, new_size - 25))
43
+ im_with_border = ImageOps.expand(im, border=3, fill=(255,255,255))
44
+ return np.array(im_with_border)
45
+
46
+ sample_images = ["23.jpg", "28.png", "35.jpg", "36w.jpg" "46.jpg", "64.jpg"]
47
+
48
+
49
  sample_images = {
50
  "23.jpg": "23.jpg",
51
  "28.png": "28.png",
 
59
  if uploaded_image is not None:
60
  image = uploaded_image[:, :, :3]
61
  else:
62
+ image = cv2.imread(sample_image_selection)
63
+
 
 
 
 
64
  image = cv2.resize(image, (512, 512))
65
  hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
66
  hue = 60
 
68
  lower_hsv = np.array([lower_hue, 50, 20])
69
  upper_hsv = np.array([upper_hue, 255, 255])
70
  mask = cv2.inRange(hsv, lower_hsv, upper_hsv)
71
+ count, result_frame = search_contours(mask, image, source="image")
72
  cv2.putText(result_frame, f'Total pip count is: {count}', (10, result_frame.shape[0] - 30), font, 1, (0, 255, 255), 2, cv2.LINE_AA)
73
+ return result_frame
 
74
 
75
  iface = gr.Interface(
76
  detect_pips,
77
  inputs=[
78
+ gr.inputs.Image(type="numpy", label="Upload Image", source="upload", optional=True), # Image upload
79
+ gr.inputs.Dropdown(choices=list(sample_images.keys()), label="Or Select a Sample Image", default=None), # Dropdown for sample images
80
  gr.inputs.Slider(label="Hue Threshold", minimum=0, maximum=500, step=1, default=250)
81
  ],
82
+ outputs=gr.outputs.Image(type="numpy", label="Result"),
 
 
 
83
  title='<b><font size="4">🁫Image Processing demonstration using OpenCV and Python:🁫</b>',
84
  description='<div style="text-align:center;"><b style="font-size:20px;">App to help you keep score when playing Dominos</b><br>Use your iPad to take a photo and upload to calculate a score<br>Note: lighting and camera angles are key factors :)</div>',
85
  allow_flagging=False,
 
87
  theme="dark",
88
  )
89
 
90
+ # Launch the Gradio interface if the script is run as the main program
91
+ if __name__ == '__main__':
92
+ iface.launch()