Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -656,19 +656,55 @@ def upload_file_to_memory():
|
|
656 |
return jsonify({"error": str(e)}), 500
|
657 |
|
658 |
# 📤 Маршрут для получения последнего изображения из памяти
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
659 |
@app.route('/last_image', methods=['GET'])
|
660 |
def get_last_image():
|
661 |
if latest_image["data"] is None:
|
662 |
return jsonify({"error": "No image available"}), 404
|
663 |
|
664 |
-
#
|
665 |
latest_image["data"].seek(0)
|
666 |
image_copy = BytesIO(latest_image["data"].read())
|
667 |
image_copy.seek(0)
|
|
|
668 |
|
|
|
|
|
669 |
return send_file(image_copy, mimetype='image/jpeg', download_name=latest_image["filename"])
|
670 |
|
671 |
-
|
|
|
|
|
672 |
|
673 |
|
674 |
@app.route('/view_image', methods=['GET'])
|
|
|
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 |
+
|
670 |
+
# Преобразуем в HSV
|
671 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
672 |
+
|
673 |
+
# Диапазоны цветов в HSV
|
674 |
+
green_mask = cv2.inRange(hsv, (36, 40, 40), (86, 255, 255))
|
675 |
+
yellow_mask = cv2.inRange(hsv, (15, 40, 40), (35, 255, 255))
|
676 |
+
brown_mask = cv2.inRange(hsv, (10, 100, 20), (20, 255, 200))
|
677 |
+
|
678 |
+
total_pixels = img.shape[0] * img.shape[1]
|
679 |
+
|
680 |
+
green_percent = int((cv2.countNonZero(green_mask) / total_pixels) * 100)
|
681 |
+
yellow_percent = int((cv2.countNonZero(yellow_mask) / total_pixels) * 100)
|
682 |
+
brown_percent = int((cv2.countNonZero(brown_mask) / total_pixels) * 100)
|
683 |
+
|
684 |
+
return {
|
685 |
+
"green": green_percent,
|
686 |
+
"yellow": yellow_percent,
|
687 |
+
"brown": brown_percent
|
688 |
+
}
|
689 |
+
|
690 |
@app.route('/last_image', methods=['GET'])
|
691 |
def get_last_image():
|
692 |
if latest_image["data"] is None:
|
693 |
return jsonify({"error": "No image available"}), 404
|
694 |
|
695 |
+
# Анализ цвета
|
696 |
latest_image["data"].seek(0)
|
697 |
image_copy = BytesIO(latest_image["data"].read())
|
698 |
image_copy.seek(0)
|
699 |
+
latest_image["color_percentages"] = analyze_colors(BytesIO(image_copy.getvalue()))
|
700 |
|
701 |
+
# Возврат изображения
|
702 |
+
image_copy.seek(0)
|
703 |
return send_file(image_copy, mimetype='image/jpeg', download_name=latest_image["filename"])
|
704 |
|
705 |
+
@app.route('/color_stats', methods=['GET'])
|
706 |
+
def color_stats():
|
707 |
+
return jsonify(latest_image["color_percentages"])
|
708 |
|
709 |
|
710 |
@app.route('/view_image', methods=['GET'])
|