brainai-spaces commited on
Commit
324b868
ยท
verified ยท
1 Parent(s): 3ded7ef

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +12 -12
utils.py CHANGED
@@ -18,40 +18,40 @@ def predict_image(image, conf_threshold):
18
  input_image, resized_image = preprocess_data(image, input_layer)
19
  output_key = compiled_model.output("boxes")
20
  boxes = compiled_model([input_image])[output_key]
21
- # Remove zero only boxes.
22
  boxes = boxes[~np.all(boxes == 0, axis=1)]
23
  return boxes, resized_image
24
 
25
  def convert_result_to_image(bgr_image, resized_image, boxes, threshold=0.3, conf_labels=True):
26
- # Define colors for boxes and descriptions.
27
  colors = {"red": (255, 0, 0), "green": (0, 255, 0)}
28
- # Fetch the image shapes to calculate a ratio.
29
  (real_y, real_x), (resized_y, resized_x) = (
30
  bgr_image.shape[:2],
31
  resized_image.shape[:2],
32
  )
33
  ratio_x, ratio_y = real_x / resized_x, real_y / resized_y
34
 
35
- # Convert the base image from BGR to RGB format.
36
  rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
37
 
38
- # Iterate through non-zero boxes.
39
  for box in boxes:
40
- # Pick a confidence factor from the last place in an array.
41
  conf = box[-1]
42
  if conf > threshold:
43
- # Convert float to int and multiply corner position of each box by x and y ratio.
44
- # If the bounding box is found at the top of the image,
45
- # position the upper box bar little lower to make it visible on the image.
46
  (x_min, y_min, x_max, y_max) = [
47
  (int(max(corner_position * ratio_y, 10)) if idx % 2 else int(corner_position * ratio_x)) for idx, corner_position in enumerate(box[:-1])
48
  ]
49
 
50
- # Draw a box based on the position, parameters in rectangle function are: image, start_point, end_point, color, thickness.
51
  rgb_image = cv2.rectangle(rgb_image, (x_min, y_min), (x_max, y_max), colors["green"], 3)
52
 
53
- # Add text to the image based on position and confidence.
54
- # Parameters in text function are: image, text, bottom-left_corner_textfield, font, font_scale, color, thickness, line_type.
55
  if conf_labels:
56
  rgb_image = cv2.putText(
57
  rgb_image,
 
18
  input_image, resized_image = preprocess_data(image, input_layer)
19
  output_key = compiled_model.output("boxes")
20
  boxes = compiled_model([input_image])[output_key]
21
+ #0์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ์ƒ์ž ์ œ๊ฑฐ
22
  boxes = boxes[~np.all(boxes == 0, axis=1)]
23
  return boxes, resized_image
24
 
25
  def convert_result_to_image(bgr_image, resized_image, boxes, threshold=0.3, conf_labels=True):
26
+ #๋ฐ”์šด๋”ฉ ๋ฐ•์Šค์™€ ๋ผ๋ฒจ์— ๋Œ€ํ•œ ์ƒ‰์ƒ ์ •์˜
27
  colors = {"red": (255, 0, 0), "green": (0, 255, 0)}
28
+ #๋น„์œจ์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฏธ์ง€ shape ๊ฐ€์ ธ์˜ค๊ธฐ
29
  (real_y, real_x), (resized_y, resized_x) = (
30
  bgr_image.shape[:2],
31
  resized_image.shape[:2],
32
  )
33
  ratio_x, ratio_y = real_x / resized_x, real_y / resized_y
34
 
35
+ #๊ธฐ๋ณธ ์ด๋ฏธ์ง€๋ฅผ BGR์—์„œ RGB ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
36
  rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
37
 
38
+ #0์ด ์•„๋‹Œ ์ƒ์ž ๋ฐ˜๋ณต
39
  for box in boxes:
40
+ #๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์œ„์น˜์—์„œ ์‹ ๋ขฐ๋„ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
41
  conf = box[-1]
42
  if conf > threshold:
43
+ #float๋ฅผ int๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๊ฐ ์ƒ์ž์˜ ๋ชจ์„œ๋ฆฌ ์œ„์น˜๋ฅผ x, y ๋น„์œจ๋กœ ๊ณฑํ•˜๊ธฐ
44
+ #์ด๋ฏธ์ง€ ์ƒ๋‹จ์— ๋ฐ”์šด๋”ฉ ๋ฐ•๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
45
+ #์œ„์ชฝ ์ƒ์ž ๋ง‰๋Œ€๋ฅผ ์กฐ๊ธˆ ์•„๋ž˜๋กœ ์œ„์น˜์‹œ์ผœ ์ด๋ฏธ์ง€์—์„œ ๋ณด์ด๋„๋ก ์œ„์น˜ ์‹œํ‚ค๊ธฐ.
46
  (x_min, y_min, x_max, y_max) = [
47
  (int(max(corner_position * ratio_y, 10)) if idx % 2 else int(corner_position * ratio_x)) for idx, corner_position in enumerate(box[:-1])
48
  ]
49
 
50
+ #์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ๊ทธ๋ฆฌ๊ธฐ. ์‚ฌ๊ฐํ˜• ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ด๋ฏธ์ง€, ์‹œ์ž‘์ , ๋์ , ์ƒ‰์ƒ, ๋‘๊ป˜
51
  rgb_image = cv2.rectangle(rgb_image, (x_min, y_min), (x_max, y_max), colors["green"], 3)
52
 
53
+ #์œ„์น˜์™€ ์‹ ๋ขฐ๋„์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€์— ํ…์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ
54
+ #ํ…์ŠคํŠธ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜: ์ด๋ฏธ์ง€, ํ…์ŠคํŠธ, ์™ผ์ชฝ ํ•˜๋‹จ ๋ชจ์„œ๋ฆฌ ํ…์ŠคํŠธ ํ•„๋“œ, ๊ธ€๊ผด, ๊ธ€๊ผด ํฌ๊ธฐ, ์ƒ‰์ƒ, ๋‘๊ป˜, ์„  ์ข…๋ฅ˜.
55
  if conf_labels:
56
  rgb_image = cv2.putText(
57
  rgb_image,