DmitrMakeev commited on
Commit
8d8b941
·
verified ·
1 Parent(s): 5410b0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -18
app.py CHANGED
@@ -666,17 +666,14 @@ def analyze_colors(image_bytes):
666
  image_bytes.seek(0)
667
  file_bytes = np.asarray(bytearray(image_bytes.read()), dtype=np.uint8)
668
  img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
669
-
670
- # Предварительная обработка изображения
671
- img = cv2.GaussianBlur(img, (3, 3), 0) # Уменьшаем шумы
672
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
673
 
674
- # Оптимизированные диапазоны HSV
675
  color_ranges = {
676
- "green": ((35, 50, 50), (85, 255, 255)), # Зеленый
677
- "yellow": ((22, 100, 150), (32, 255, 255)), # Желтый (расширенный диапазон)
678
- "orange": ((10, 150, 150), (20, 255, 255)), # Оранжевый
679
- "brown": ((5, 50, 30), (15, 180, 180)) # Коричневый (более темный)
680
  }
681
 
682
  total_pixels = img.shape[0] * img.shape[1]
@@ -684,19 +681,9 @@ def analyze_colors(image_bytes):
684
 
685
  for color, (lower, upper) in color_ranges.items():
686
  mask = cv2.inRange(hsv, np.array(lower), np.array(upper))
687
-
688
- # Улучшаем маску морфологическими операциями
689
- kernel = np.ones((3,3), np.uint8)
690
- mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
691
-
692
  percent = round(cv2.countNonZero(mask) / total_pixels * 100, 1)
693
  results[color] = percent
694
 
695
- # Для отладки можно сохранять маски
696
- if DEBUG_MODE:
697
- for color, mask in masks.items():
698
- cv2.imwrite(f'{color}_mask.jpg', mask)
699
-
700
  return results
701
 
702
  @app.route('/last_image', methods=['GET'])
 
666
  image_bytes.seek(0)
667
  file_bytes = np.asarray(bytearray(image_bytes.read()), dtype=np.uint8)
668
  img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
 
 
 
669
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
670
 
671
+ # Оптимизированные, но безопасные диапазоны цветов
672
  color_ranges = {
673
+ "green": ((36, 40, 40), (86, 255, 255)), # Зеленый (оставил как было)
674
+ "yellow": ((22, 100, 100), (32, 255, 255)), # Желтый (немного расширил)
675
+ "orange": ((10, 120, 120), (20, 255, 255)), # Оранжевый (чуть уменьшил насыщенность)
676
+ "brown": ((5, 50, 20), (15, 150, 150)) # Коричневый (оставил почти как было)
677
  }
678
 
679
  total_pixels = img.shape[0] * img.shape[1]
 
681
 
682
  for color, (lower, upper) in color_ranges.items():
683
  mask = cv2.inRange(hsv, np.array(lower), np.array(upper))
 
 
 
 
 
684
  percent = round(cv2.countNonZero(mask) / total_pixels * 100, 1)
685
  results[color] = percent
686
 
 
 
 
 
 
687
  return results
688
 
689
  @app.route('/last_image', methods=['GET'])