Spaces:
Sleeping
Sleeping
File size: 3,471 Bytes
c601305 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
#!/usr/bin/env python
# coding: utf-8
# In[32]:
import gradio as gr
import sqlite3
import numpy as np
from PIL import Image
import io
import uuid
import cv2
# In[34]:
# Initialize Database
DATABASE = 'Ashok_database.db'
# In[36]:
def init_db():
with sqlite3.connect(DATABASE) as conn:
conn.execute('''CREATE TABLE IF NOT EXISTS images
(id TEXT PRIMARY KEY, image_blob BLOB)''')
print("Database initialized.")
init_db()
# In[38]:
# Save image as .jpg in the database
def save_image_to_db(image_array, image_id):
# Convert to .jpg format
_, buffer = cv2.imencode('.jpg', image_array)
# Save to database
with sqlite3.connect(DATABASE) as conn:
conn.execute("INSERT INTO images (id, image_blob) VALUES (?, ?)",
(image_id, buffer.tobytes()))
print(f"Image saved with ID: {image_id}")
# In[40]:
# Convert Image to Numbers
def convert_image_to_numbers(image):
image = Image.fromarray(image) # Convert to PIL image
image = image.resize((224, 224)) # Resize for consistency
image_array = np.array(image) # Convert to NumPy array
np.save("image_data.npy", image_array) # Save as .npy file
return "Image converted and saved successfully!"
# In[42]:
# Retrieve image from the database
def retrieve_image_from_db(image_id):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.execute("SELECT image_blob FROM images WHERE id = ?", (image_id,))
row = cursor.fetchone()
if row is None:
return None
# Convert binary data back to NumPy array
image_array = np.frombuffer(row[0], dtype=np.uint8)
# Decode image array to OpenCV format
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
return image
# In[44]:
# Convert PIL Image to OpenCV (NumPy array)
def pil_to_cv2(image):
return cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
# In[46]:
# Convert OpenCV (NumPy array) to PIL Image
def cv2_to_pil(image_array):
return Image.fromarray(cv2.cvtColor(image_array, cv2.COLOR_BGR2RGB))
# In[ ]:
# In[49]:
def upload(image):
image_array = pil_to_cv2(image)
image_id = str(uuid.uuid4())
save_image_to_db(image_array, image_id)
return f"Image successfully uploaded with ID: {image_id}"
# In[51]:
# Retrieve function to display image
def retrieve(image_id):
image_array = retrieve_image_from_db(image_id)
if image_array is None:
return f"No image found with ID: {image_id}", None
retrieved_image = cv2_to_pil(image_array)
return f"Image successfully retrieved with ID: {image_id}", retrieved_image
# In[59]:
# Gradio Interfaces
upload_interface = gr.Interface(
fn=upload,
inputs=gr.Image(type="pil"),
outputs="text",
title="Upload and Save Image as .jpg",
description="Upload an image to save it as .jpg format in the database."
)
retrieve_interface = gr.Interface(
fn=retrieve,
inputs="text",
outputs=["text", "image"],
title="Retrieve Image from Database",
description="Enter the unique ID to retrieve the saved image from the database."
)
# Combine Interfaces
app = gr.TabbedInterface([upload_interface, retrieve_interface], ["Upload", "Retrieve"])
app.launch(share=True)
# In[49]:
# In[ ]:
|