pirahansiah commited on
Commit
6700e87
1 Parent(s): 598166e

update threshodling methods

Browse files
__pycache__/threshold_methods.cpython-310.pyc CHANGED
Binary files a/__pycache__/threshold_methods.cpython-310.pyc and b/__pycache__/threshold_methods.cpython-310.pyc differ
 
app.py CHANGED
@@ -3,11 +3,13 @@ from threshold_methods import threshold_methods
3
  import cv2
4
 
5
  new_outputs = [
6
- gr.outputs.Image(type="numpy", label="Output Image")
 
7
  ]
8
  def show_image():
9
- img = cv2.imread('huggingface.png')
10
- return img
 
11
 
12
  HuggingFace = gr.Interface(
13
  fn=show_image,
@@ -20,7 +22,7 @@ HuggingFace = gr.Interface(
20
  show_clear=False,
21
  show_generate=False,
22
  allow_flagging=False,
23
- title="https://huggingface.co/spaces/pirahansiah/ComputerVision",
24
  )
25
 
26
  gr.TabbedInterface(
 
3
  import cv2
4
 
5
  new_outputs = [
6
+ gr.outputs.Image(type="numpy", label="Output Image"),
7
+ gr.outputs.Textbox(type="text", label="My HuggingFace URL")
8
  ]
9
  def show_image():
10
+ img = cv2.imread('huggingface.png')
11
+ text = 'https://huggingface.co/spaces/pirahansiah/ComputerVision'
12
+ return img,text
13
 
14
  HuggingFace = gr.Interface(
15
  fn=show_image,
 
22
  show_clear=False,
23
  show_generate=False,
24
  allow_flagging=False,
25
+ title="Computer Vision and Deep Learning by Farshid PirahanSiah",
26
  )
27
 
28
  gr.TabbedInterface(
gitignore CHANGED
@@ -1,6 +1,5 @@
1
  flagged/
2
  *.pt
3
- *.png
4
  *.jpg
5
  *.mp4
6
  *.mkv
 
1
  flagged/
2
  *.pt
 
3
  *.jpg
4
  *.mp4
5
  *.mkv
threshold_methods.py CHANGED
@@ -8,7 +8,7 @@ def pirahansiah_threshold_method_find_threshold_values_2(grayImg):
8
  #https://pdfs.semanticscholar.org/05b2/d39fce4e8a99897e95f8c75416f65a5a0acc.pdf
9
  assert grayImg is not None, "file could not be read, check with os.path.exists()"
10
  #img = cv2.GaussianBlur(self.grayImg, (3, 3), 0)
11
- img = grayImg
12
  # Initialize an array to store the PSNR values for each threshold value
13
  psnr_values = np.zeros(256)
14
  psnr_max=0
@@ -16,9 +16,9 @@ def pirahansiah_threshold_method_find_threshold_values_2(grayImg):
16
  # Iterate over all possible threshold values with a step size of 5
17
  for t in range(0, 256, 5):
18
  # Threshold the image using the current threshold value
19
- _, binary = cv2.threshold(img, t, 255, cv2.THRESH_BINARY)
20
  # Calculate the PSNR between the binary image and the original image
21
- psnr = cv2.PSNR(binary, img)
22
  # Store the PSNR value
23
  psnr_values[t] = psnr
24
  if (psnr_max<psnr):
@@ -29,7 +29,7 @@ def pirahansiah_threshold_method_find_threshold_values_2(grayImg):
29
  th=int(th/mean_psnr)
30
  # Find the threshold values that satisfy the condition
31
  thresh = th #np.argwhere((mean_psnr / k1 < psnr_values) & (psnr_values < mean_psnr / k2)).flatten()
32
-
33
  return thresh
34
  def pirahansiah_threshold_method_find_threshold_values_1(grayImg):
35
  #https://www.jatit.org/volumes/Vol57No2/4Vol57No2.pdf
@@ -51,12 +51,11 @@ def pirahansiah_threshold_method_find_threshold_values_1(grayImg):
51
 
52
 
53
 
54
- path = [['image_0.png']]
55
  inputs_thresh = [
56
  gr.inputs.Image(type="filepath", label="Input Image"),
57
- gr.components.Slider(label="Manual Threshold Value", value=125, minimum=10, maximum=255, step=5),
58
  gr.inputs.Radio(label="Threshold Methods",
59
- choices=[
60
  "cv2.threshold(grayImg, 128, 255, cv2.THRESH_BINARY)"
61
  ,"cv2.threshold(grayImg, 128, 255, cv2.THRESH_BINARY_INV)"
62
  ,"cv2.threshold(grayImg, 128, 255, cv2.THRESH_TRUNC)"
@@ -66,17 +65,20 @@ gr.inputs.Radio(label="Threshold Methods",
66
  ,"cv2.threshold(grayImg, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU,)"
67
  ,"Adapted from PirahanSiah Threshold Method I derivative demo"
68
  ,"Inspired by PirahanSiah Threshold Method II derivative demo"
 
69
  ]),
 
70
  ]
71
 
72
  outputs_thresh = [
73
- gr.outputs.Image(type="numpy", label="Output Image")
 
74
  ]
75
 
76
- def process_image(input_image, slider_val, radio_choice):
77
- img = cv2.imread(input_image,0)
78
- _, binaryImg = cv2.threshold(img, slider_val, 255, cv2.THRESH_BINARY)
79
-
80
  if radio_choice == "cv2.threshold(grayImg, 128, 255, cv2.THRESH_BINARY)":
81
  _, binaryImg=cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)
82
  elif radio_choice == "cv2.threshold(grayImg, 128, 255, cv2.THRESH_BINARY_INV)":
@@ -96,10 +98,12 @@ def process_image(input_image, slider_val, radio_choice):
96
  _, binaryImg = cv2.threshold(img, threshval, 255, cv2.THRESH_BINARY)
97
  elif radio_choice == "Inspired by PirahanSiah Threshold Method II derivative demo":
98
  threshval=pirahansiah_threshold_method_find_threshold_values_2(img)
99
- _, binaryImg = cv2.threshold(img, threshval, 255, cv2.THRESH_BINARY)
 
 
100
  return binaryImg
101
 
102
- def on_change(slider_val, radio_choice):
103
  # Update output
104
  outputs_thresh[0].update(process_image(
105
  inputs_thresh[0].value,
 
8
  #https://pdfs.semanticscholar.org/05b2/d39fce4e8a99897e95f8c75416f65a5a0acc.pdf
9
  assert grayImg is not None, "file could not be read, check with os.path.exists()"
10
  #img = cv2.GaussianBlur(self.grayImg, (3, 3), 0)
11
+ image = grayImg
12
  # Initialize an array to store the PSNR values for each threshold value
13
  psnr_values = np.zeros(256)
14
  psnr_max=0
 
16
  # Iterate over all possible threshold values with a step size of 5
17
  for t in range(0, 256, 5):
18
  # Threshold the image using the current threshold value
19
+ _, binary = cv2.threshold(image, t, 255, cv2.THRESH_BINARY)
20
  # Calculate the PSNR between the binary image and the original image
21
+ psnr = cv2.PSNR(binary, image)
22
  # Store the PSNR value
23
  psnr_values[t] = psnr
24
  if (psnr_max<psnr):
 
29
  th=int(th/mean_psnr)
30
  # Find the threshold values that satisfy the condition
31
  thresh = th #np.argwhere((mean_psnr / k1 < psnr_values) & (psnr_values < mean_psnr / k2)).flatten()
32
+ print(th)
33
  return thresh
34
  def pirahansiah_threshold_method_find_threshold_values_1(grayImg):
35
  #https://www.jatit.org/volumes/Vol57No2/4Vol57No2.pdf
 
51
 
52
 
53
 
54
+ path = [['image_0.png'],['huggingface.png']]
55
  inputs_thresh = [
56
  gr.inputs.Image(type="filepath", label="Input Image"),
 
57
  gr.inputs.Radio(label="Threshold Methods",
58
+ choices=[
59
  "cv2.threshold(grayImg, 128, 255, cv2.THRESH_BINARY)"
60
  ,"cv2.threshold(grayImg, 128, 255, cv2.THRESH_BINARY_INV)"
61
  ,"cv2.threshold(grayImg, 128, 255, cv2.THRESH_TRUNC)"
 
65
  ,"cv2.threshold(grayImg, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU,)"
66
  ,"Adapted from PirahanSiah Threshold Method I derivative demo"
67
  ,"Inspired by PirahanSiah Threshold Method II derivative demo"
68
+ ,"Manual Threshold Value select by Slider"
69
  ]),
70
+ gr.components.Slider(label="Manual Threshold Value", value=125, minimum=10, maximum=255, step=5),
71
  ]
72
 
73
  outputs_thresh = [
74
+ gr.outputs.Image(type="numpy", label="Output Threshold Image")
75
+
76
  ]
77
 
78
+ def process_image(input_image, radio_choice,slider_val):
79
+ img = cv2.imread(input_image,0)
80
+ binaryImg=img.copy()
81
+ print(radio_choice)
82
  if radio_choice == "cv2.threshold(grayImg, 128, 255, cv2.THRESH_BINARY)":
83
  _, binaryImg=cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)
84
  elif radio_choice == "cv2.threshold(grayImg, 128, 255, cv2.THRESH_BINARY_INV)":
 
98
  _, binaryImg = cv2.threshold(img, threshval, 255, cv2.THRESH_BINARY)
99
  elif radio_choice == "Inspired by PirahanSiah Threshold Method II derivative demo":
100
  threshval=pirahansiah_threshold_method_find_threshold_values_2(img)
101
+ _, binaryImg = cv2.threshold(img, threshval, 255, cv2.THRESH_BINARY)
102
+ elif radio_choice == "Manual Threshold Value select by Slider":
103
+ _, binaryImg = cv2.threshold(img, slider_val, 255, cv2.THRESH_BINARY)
104
  return binaryImg
105
 
106
+ def on_change(radio_choice,slider_val):
107
  # Update output
108
  outputs_thresh[0].update(process_image(
109
  inputs_thresh[0].value,