DmitrMakeev commited on
Commit
fa0d50d
·
verified ·
1 Parent(s): a9e97c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -2
app.py CHANGED
@@ -13,6 +13,10 @@ import uuid
13
  from io import BytesIO
14
 
15
 
 
 
 
 
16
  from werkzeug.utils import secure_filename
17
 
18
 
@@ -652,19 +656,62 @@ def upload_file_to_memory():
652
  return jsonify({"error": str(e)}), 500
653
 
654
  # 📤 Маршрут для получения последнего изображения из памяти
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
655
  @app.route('/last_image', methods=['GET'])
656
  def get_last_image():
657
  if latest_image["data"] is None:
658
  return jsonify({"error": "No image available"}), 404
659
 
660
- # Создаем копию потока
661
  latest_image["data"].seek(0)
662
  image_copy = BytesIO(latest_image["data"].read())
663
  image_copy.seek(0)
 
664
 
 
 
665
  return send_file(image_copy, mimetype='image/jpeg', download_name=latest_image["filename"])
666
 
667
- return send_file(latest_image["data"], mimetype='image/jpeg', download_name=latest_image["filename"])
 
 
 
 
 
 
 
 
 
 
668
 
669
 
670
 
 
13
  from io import BytesIO
14
 
15
 
16
+ import cv2
17
+ import numpy as np
18
+
19
+
20
  from werkzeug.utils import secure_filename
21
 
22
 
 
656
  return jsonify({"error": str(e)}), 500
657
 
658
  # 📤 Маршрут для получения последнего изображения из памяти
659
+ latest_image = {
660
+ "data": None,
661
+ "filename": "",
662
+ "color_percentages": {"green": 0, "yellow": 0, "brown": 0}
663
+ }
664
+
665
+ 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
+ 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]
680
+ results = {}
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'])
690
  def get_last_image():
691
  if latest_image["data"] is None:
692
  return jsonify({"error": "No image available"}), 404
693
 
694
+ # Анализ цвета
695
  latest_image["data"].seek(0)
696
  image_copy = BytesIO(latest_image["data"].read())
697
  image_copy.seek(0)
698
+ latest_image["color_percentages"] = analyze_colors(BytesIO(image_copy.getvalue()))
699
 
700
+ # Возврат изображения
701
+ image_copy.seek(0)
702
  return send_file(image_copy, mimetype='image/jpeg', download_name=latest_image["filename"])
703
 
704
+ @app.route('/color_stats')
705
+ def color_stats():
706
+ stats = latest_image["color_percentages"]
707
+ print(f"Raw color stats: {stats}") # Для отладки в консоли
708
+ return jsonify(stats)
709
+
710
+
711
+ @app.route('/view_image', methods=['GET'])
712
+ def view_image():
713
+ return render_template('show_image.html')
714
+
715
 
716
 
717