Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
import telebot
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Телеграм-токен
|
| 9 |
+
API_TOKEN = '7458760921:AAFEMNVBuRcM_txnumnwl48u6MlKUS0J4YM'
|
| 10 |
+
bot = telebot.TeleBot(API_TOKEN)
|
| 11 |
+
|
| 12 |
+
# Создание папок для хранения файлов
|
| 13 |
+
os.makedirs('uploads', exist_ok=True)
|
| 14 |
+
os.makedirs('processed', exist_ok=True)
|
| 15 |
+
|
| 16 |
+
# Загрузка классификатора для распознавания лиц
|
| 17 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 18 |
+
|
| 19 |
+
# Функция для обработки изображения
|
| 20 |
+
def process_image(image_path):
|
| 21 |
+
img = cv2.imread(image_path)
|
| 22 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 23 |
+
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 24 |
+
|
| 25 |
+
pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 26 |
+
draw = ImageDraw.Draw(pil_img)
|
| 27 |
+
|
| 28 |
+
coords = []
|
| 29 |
+
for (x, y, w, h) in faces:
|
| 30 |
+
draw.rectangle([x, y, x + w, y + h], outline="green", width=2)
|
| 31 |
+
coords.append((x, y, w, h))
|
| 32 |
+
draw.text((x, y - 10), f"({x}, {y})", fill="green")
|
| 33 |
+
|
| 34 |
+
processed_path = image_path.replace('uploads', 'processed')
|
| 35 |
+
pil_img.save(processed_path)
|
| 36 |
+
return processed_path, coords
|
| 37 |
+
|
| 38 |
+
# Обработчик сообщений с изображениями
|
| 39 |
+
@bot.message_handler(content_types=['photo'])
|
| 40 |
+
def handle_photo(message):
|
| 41 |
+
file_info = bot.get_file(message.photo[-1].file_id)
|
| 42 |
+
downloaded_file = bot.download_file(file_info.file_path)
|
| 43 |
+
|
| 44 |
+
file_name = f"uploads/{message.photo[-1].file_id}.jpg"
|
| 45 |
+
with open(file_name, 'wb') as f:
|
| 46 |
+
f.write(downloaded_file)
|
| 47 |
+
|
| 48 |
+
processed_path, coords = process_image(file_name)
|
| 49 |
+
|
| 50 |
+
# Отправка обработанного изображения с координатами
|
| 51 |
+
with open(processed_path, 'rb') as f:
|
| 52 |
+
bot.send_photo(message.chat.id, f, caption=f"Координаты лиц: {coords}")
|
| 53 |
+
|
| 54 |
+
# Gradio интерфейс
|
| 55 |
+
def view_files():
|
| 56 |
+
files = os.listdir('processed')
|
| 57 |
+
return {file: f"processed/{file}" for file in files}
|
| 58 |
+
|
| 59 |
+
def delete_file(file_name):
|
| 60 |
+
file_path = f"processed/{file_name}"
|
| 61 |
+
if os.path.exists(file_path):
|
| 62 |
+
os.remove(file_path)
|
| 63 |
+
return f"Файл {file_name} удалён."
|
| 64 |
+
return f"Файл {file_name} не найден."
|
| 65 |
+
|
| 66 |
+
with gr.Blocks() as gr_interface:
|
| 67 |
+
gr.Markdown("## Управление обработанными изображениями")
|
| 68 |
+
with gr.Row():
|
| 69 |
+
gallery = gr.Gallery(label="Обработанные файлы").style(grid=3)
|
| 70 |
+
with gr.Row():
|
| 71 |
+
delete_button = gr.Button("Удалить выбранный файл")
|
| 72 |
+
delete_input = gr.Textbox(label="Имя файла для удаления")
|
| 73 |
+
delete_output = gr.Textbox(label="Результат удаления")
|
| 74 |
+
|
| 75 |
+
delete_button.click(delete_file, inputs=delete_input, outputs=delete_output)
|
| 76 |
+
gr.Interface(view_files, [], gallery).launch()
|
| 77 |
+
|
| 78 |
+
# Запуск бота
|
| 79 |
+
bot.polling()
|