mirror of
https://github.com/Paillat-dev/Botator.git
synced 2026-01-02 01:06:19 +00:00
[VISION] Safed vision processing, not mandatory
This commit is contained in:
@@ -15,52 +15,53 @@ except:
|
|||||||
|
|
||||||
|
|
||||||
async def process(attachment):
|
async def process(attachment):
|
||||||
if not os.path.exists("./../database/google-vision"):
|
|
||||||
debug("Google Vision API is not setup, please run /setup")
|
|
||||||
return
|
|
||||||
debug("Processing image...")
|
|
||||||
image = vision.Image()
|
|
||||||
image.source.image_uri = attachment.url
|
|
||||||
labels = client.label_detection(image=image)
|
|
||||||
texts = client.text_detection(image=image)
|
|
||||||
objects = client.object_localization(image=image)
|
|
||||||
labels = labels.label_annotations
|
|
||||||
texts = texts.text_annotations
|
|
||||||
objects = objects.localized_object_annotations
|
|
||||||
# we take the first 4 labels and the first 4 objects
|
|
||||||
labels = labels[:2]
|
|
||||||
objects = objects[:7]
|
|
||||||
final = "<image\n"
|
|
||||||
if len(labels) > 0:
|
|
||||||
final += "Labels:\n"
|
|
||||||
for label in labels:
|
|
||||||
final += label.description + ", "
|
|
||||||
final = final[:-2] + "\n"
|
|
||||||
if len(texts) > 0:
|
|
||||||
final += "Text:\n"
|
|
||||||
try:
|
try:
|
||||||
final += (
|
debug("Processing image...")
|
||||||
texts[0].description + "\n"
|
image = vision.Image()
|
||||||
) # we take the first text, wich is the whole text in reality
|
image.source.image_uri = attachment.url
|
||||||
except:
|
labels = client.label_detection(image=image)
|
||||||
pass
|
texts = client.text_detection(image=image)
|
||||||
if len(objects) > 0:
|
objects = client.object_localization(image=image)
|
||||||
final += "Objects:\n"
|
labels = labels.label_annotations
|
||||||
for obj in objects:
|
texts = texts.text_annotations
|
||||||
final += obj.name + ", "
|
objects = objects.localized_object_annotations
|
||||||
final = final[:-2] + "\n"
|
# we take the first 4 labels and the first 4 objects
|
||||||
final += "!image>"
|
labels = labels[:2]
|
||||||
# we store the result in a file called attachment.key.txt in the folder ./../database/google-vision/results
|
objects = objects[:7]
|
||||||
# we create the folder if it doesn't exist
|
final = "<image\n"
|
||||||
if not os.path.exists("./../database/google-vision/results"):
|
if len(labels) > 0:
|
||||||
os.mkdir("./../database/google-vision/results")
|
final += "Labels:\n"
|
||||||
# we create the file
|
for label in labels:
|
||||||
with open(
|
final += label.description + ", "
|
||||||
f"./../database/google-vision/results/{attachment.id}.txt",
|
final = final[:-2] + "\n"
|
||||||
"w",
|
if len(texts) > 0:
|
||||||
encoding="utf-8",
|
final += "Text:\n"
|
||||||
) as f:
|
try:
|
||||||
f.write(final)
|
final += (
|
||||||
f.close()
|
texts[0].description + "\n"
|
||||||
|
) # we take the first text, wich is the whole text in reality
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
if len(objects) > 0:
|
||||||
|
final += "Objects:\n"
|
||||||
|
for obj in objects:
|
||||||
|
final += obj.name + ", "
|
||||||
|
final = final[:-2] + "\n"
|
||||||
|
final += "!image>"
|
||||||
|
# we store the result in a file called attachment.key.txt in the folder ./../database/google-vision/results
|
||||||
|
# we create the folder if it doesn't exist
|
||||||
|
if not os.path.exists("./../database/google-vision/results"):
|
||||||
|
os.mkdir("./../database/google-vision/results")
|
||||||
|
# we create the file
|
||||||
|
with open(
|
||||||
|
f"./../database/google-vision/results/{attachment.id}.txt",
|
||||||
|
"w",
|
||||||
|
encoding="utf-8",
|
||||||
|
) as f:
|
||||||
|
f.write(final)
|
||||||
|
f.close()
|
||||||
|
|
||||||
return final
|
return final
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
debug("Error while processing image: " + str(e))
|
||||||
|
|||||||
Reference in New Issue
Block a user