This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97bca0e
commit 8d6b159
Showing
8 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import os | ||
import requests | ||
import json | ||
import base64 | ||
import io | ||
from PIL import Image | ||
|
||
|
||
def encode_image_to_base64(image): | ||
# if image is str convert to BytesIO | ||
if isinstance(image, str): | ||
with Image.open(image) as img: | ||
buffered = io.BytesIO() | ||
img.save(buffered, format="JPEG") | ||
|
||
bytes_data = buffered.getvalue() | ||
|
||
else: | ||
bytes_data = image.getvalue() | ||
|
||
return base64.b64encode(bytes_data).decode("utf-8") | ||
|
||
|
||
def vision_ai_classify_image(base64_image): | ||
# read OPENAI_API_KEY env | ||
api_key = os.environ.get("OPENAI_API_KEY") | ||
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"} | ||
|
||
payload = { | ||
"model": "gpt-4-vision-preview", | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "text", | ||
"text": """ | ||
You are an expert flooding detector. | ||
You are given a image. You must detect if there is flooding in the image. | ||
the output MUST be a json object with a boolean value for the key "flooding_detected". | ||
If you don't know what to anwser, you can set the key "flooding_detect" as false. | ||
Example: | ||
{ | ||
"flooding_detected": true | ||
} | ||
""", | ||
}, | ||
{ | ||
"type": "image_url", | ||
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}, | ||
}, | ||
], | ||
} | ||
], | ||
"max_tokens": 300, | ||
} | ||
|
||
response = requests.post( | ||
"https://api.openai.com/v1/chat/completions", headers=headers, json=payload | ||
) | ||
return response.json() | ||
|
||
|
||
def get_ai_label(response): | ||
if response.get("error"): | ||
return "Error" | ||
else: | ||
r = response["choices"][0]["message"]["content"] | ||
json_string = r.replace("```json\n", "").replace("\n```", "") | ||
json_object = json.loads(json_string) | ||
return json_object["flooding_detected"] | ||
|
||
|
||
def run_model(img): | ||
base64_image = encode_image_to_base64(img) | ||
|
||
response = vision_ai_classify_image(base64_image) | ||
|
||
return get_ai_label(response) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import streamlit as st | ||
from PIL import Image | ||
|
||
from model import run_model | ||
|
||
|
||
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB | ||
|
||
st.set_page_config(layout="wide", page_title="Experimente o Modelo") | ||
|
||
st.markdown("# Identifique um alagamento por uma imagem") | ||
|
||
my_upload = st.file_uploader("Faça upload de uma imagem", type=["png", "jpg", "jpeg"]) | ||
|
||
if my_upload is not None: | ||
if my_upload.size > MAX_FILE_SIZE: | ||
st.error( | ||
"The uploaded file is too large. Please upload an image smaller than 5MB." | ||
) | ||
else: | ||
res = run_model(my_upload) | ||
else: | ||
my_upload = "./data/imgs/flooded1.jpg" | ||
res = run_model(my_upload) | ||
|
||
if res: | ||
st.markdown("### Alagamento Identificado ✅") | ||
else: | ||
st.markdown("### Alagamento Não Identificado ❌") | ||
|
||
st.image(my_upload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.