Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import requests
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
import openai
|
|
|
|
|
|
|
| 7 |
|
| 8 |
prompt = "Type and press Enter"
|
| 9 |
|
|
@@ -11,6 +13,18 @@ def encode_image(image_path):
|
|
| 11 |
with open(image_path, "rb") as image_file:
|
| 12 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def api_calling(image, prompt, api_key):
|
| 15 |
base64_image = encode_image(image)
|
| 16 |
if len(prompt) == 0:
|
|
@@ -66,16 +80,19 @@ block = gr.Blocks(theme=gr.themes.Glass(primary_hue="slate"))
|
|
| 66 |
with block:
|
| 67 |
gr.Markdown("""<h1><center>Visual Explorer - Ask and Learn about Images</center></h1> """)
|
| 68 |
with gr.Row():
|
| 69 |
-
with gr.Column(
|
| 70 |
img_input = gr.Image(type="filepath", label="Upload Image")
|
| 71 |
api_input = gr.Textbox(label= "Enter Api-key")
|
| 72 |
upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
|
|
|
|
|
|
|
| 73 |
with gr.Column():
|
| 74 |
chatbot = gr.Chatbot(label="Chat with Image")
|
| 75 |
message = gr.Textbox(label="User", placeholder=prompt)
|
| 76 |
state = gr.State()
|
| 77 |
|
| 78 |
upload_button.click(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
|
|
|
|
| 79 |
message.submit(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
|
| 80 |
message.submit(lambda: None, None, message, queue=False)
|
| 81 |
block.launch()
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
import openai
|
| 7 |
+
import cv2
|
| 8 |
+
import numpy
|
| 9 |
|
| 10 |
prompt = "Type and press Enter"
|
| 11 |
|
|
|
|
| 13 |
with open(image_path, "rb") as image_file:
|
| 14 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 15 |
|
| 16 |
+
def mark_contours(image_path):
|
| 17 |
+
image = cv2.imread(image_path)
|
| 18 |
+
image = cv2.resize(image, (0, 0), fx = 0.5, fy = 0.5)
|
| 19 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 20 |
+
ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
|
| 21 |
+
contours, hierarchy = cv2.findContours(binary, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
|
| 22 |
+
image_copy = image.copy()
|
| 23 |
+
image_copy = cv2.drawContours(image_copy, contours, -1, (0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
|
| 24 |
+
cv2.imwrite('cont_image.jpg', image_copy)
|
| 25 |
+
return 'cont_image.jpg'
|
| 26 |
+
|
| 27 |
+
|
| 28 |
def api_calling(image, prompt, api_key):
|
| 29 |
base64_image = encode_image(image)
|
| 30 |
if len(prompt) == 0:
|
|
|
|
| 80 |
with block:
|
| 81 |
gr.Markdown("""<h1><center>Visual Explorer - Ask and Learn about Images</center></h1> """)
|
| 82 |
with gr.Row():
|
| 83 |
+
with gr.Column():
|
| 84 |
img_input = gr.Image(type="filepath", label="Upload Image")
|
| 85 |
api_input = gr.Textbox(label= "Enter Api-key")
|
| 86 |
upload_button = gr.Button(value="Upload & Start Chat", interactive=True, variant="primary")
|
| 87 |
+
with gr.Column():
|
| 88 |
+
output_img = gr.Image(type="filepath",label="Selected Images")
|
| 89 |
with gr.Column():
|
| 90 |
chatbot = gr.Chatbot(label="Chat with Image")
|
| 91 |
message = gr.Textbox(label="User", placeholder=prompt)
|
| 92 |
state = gr.State()
|
| 93 |
|
| 94 |
upload_button.click(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
|
| 95 |
+
upload_button.click(mark_contours, inputs=image_name, outputs=output_img)
|
| 96 |
message.submit(message_and_history, inputs=[img_input, message, state, api_input], outputs=[chatbot, state])
|
| 97 |
message.submit(lambda: None, None, message, queue=False)
|
| 98 |
block.launch()
|