Commit
·
2a13288
1
Parent(s):
9cfa877
Subindo arquivos3
Browse files- app.py +9 -9
- requirements.txt +1 -1
app.py
CHANGED
@@ -7,17 +7,17 @@ import cv2
|
|
7 |
from skimage.feature import greycomatrix, greycoprops
|
8 |
from transformers import BlipProcessor, BlipForConditionalGeneration, MarianMTModel, MarianTokenizer
|
9 |
|
10 |
-
#
|
11 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
|
12 |
|
13 |
-
#análise de textura usando GLCM
|
14 |
def analyze_texture(image):
|
15 |
gray_image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
|
16 |
glcm = greycomatrix(gray_image, distances=[5], angles=[0], levels=256, symmetric=True, normed=True)
|
17 |
contrast = greycoprops(glcm, 'contrast')[0, 0]
|
18 |
return contrast
|
19 |
|
20 |
-
#
|
21 |
def describe_image(image):
|
22 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
23 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
@@ -26,7 +26,7 @@ def describe_image(image):
|
|
26 |
description = processor.decode(out[0], skip_special_tokens=True)
|
27 |
return description
|
28 |
|
29 |
-
#
|
30 |
def translate_description(description):
|
31 |
model_name = 'Helsinki-NLP/opus-mt-en-pt'
|
32 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
@@ -35,9 +35,9 @@ def translate_description(description):
|
|
35 |
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
36 |
return translated_text
|
37 |
|
38 |
-
#
|
39 |
def process_image(image):
|
40 |
-
# Detecção de
|
41 |
results = model(image)
|
42 |
detected_image = results.render()[0]
|
43 |
|
@@ -55,13 +55,13 @@ def process_image(image):
|
|
55 |
tts = gTTS(text=translated_description, lang='pt')
|
56 |
tts.save("output.mp3")
|
57 |
|
58 |
-
#
|
59 |
return Image.fromarray(detected_image), translated_description, "output.mp3"
|
60 |
|
61 |
-
#
|
62 |
example_image = Image.open("/mnt/data/example1.JPG")
|
63 |
|
64 |
-
#Gradio
|
65 |
iface = gr.Interface(
|
66 |
fn=process_image,
|
67 |
inputs=gr.inputs.Image(type="pil"),
|
|
|
7 |
from skimage.feature import greycomatrix, greycoprops
|
8 |
from transformers import BlipProcessor, BlipForConditionalGeneration, MarianMTModel, MarianTokenizer
|
9 |
|
10 |
+
# Carregar o modelo YOLOv5
|
11 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
|
12 |
|
13 |
+
# Função para análise de textura usando GLCM
|
14 |
def analyze_texture(image):
|
15 |
gray_image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
|
16 |
glcm = greycomatrix(gray_image, distances=[5], angles=[0], levels=256, symmetric=True, normed=True)
|
17 |
contrast = greycoprops(glcm, 'contrast')[0, 0]
|
18 |
return contrast
|
19 |
|
20 |
+
# Função para descrever imagem usando BLIP
|
21 |
def describe_image(image):
|
22 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
23 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
26 |
description = processor.decode(out[0], skip_special_tokens=True)
|
27 |
return description
|
28 |
|
29 |
+
# Função para traduzir descrição para português
|
30 |
def translate_description(description):
|
31 |
model_name = 'Helsinki-NLP/opus-mt-en-pt'
|
32 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
|
|
35 |
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
36 |
return translated_text
|
37 |
|
38 |
+
# Função principal para processar imagem e gerar saída de voz
|
39 |
def process_image(image):
|
40 |
+
# Detecção de objetos
|
41 |
results = model(image)
|
42 |
detected_image = results.render()[0]
|
43 |
|
|
|
55 |
tts = gTTS(text=translated_description, lang='pt')
|
56 |
tts.save("output.mp3")
|
57 |
|
58 |
+
# Retornar imagem com detecções, descrição e áudio
|
59 |
return Image.fromarray(detected_image), translated_description, "output.mp3"
|
60 |
|
61 |
+
# Carregar imagem de exemplo
|
62 |
example_image = Image.open("/mnt/data/example1.JPG")
|
63 |
|
64 |
+
# Interface Gradio
|
65 |
iface = gr.Interface(
|
66 |
fn=process_image,
|
67 |
inputs=gr.inputs.Image(type="pil"),
|
requirements.txt
CHANGED
@@ -3,7 +3,7 @@ torch
|
|
3 |
Pillow
|
4 |
numpy
|
5 |
opencv-python
|
6 |
-
scikit-image
|
7 |
transformers
|
8 |
gtts
|
9 |
|
|
|
3 |
Pillow
|
4 |
numpy
|
5 |
opencv-python
|
6 |
+
scikit-image>=0.18.3
|
7 |
transformers
|
8 |
gtts
|
9 |
|