Spaces:
Running
Running
Commit
·
06ad0a5
1
Parent(s):
868f127
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,15 @@ from PIL import Image
|
|
| 5 |
import urllib.request
|
| 6 |
import io
|
| 7 |
from utils import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
# Initialize labels and model
|
|
@@ -25,8 +34,49 @@ st.markdown('''
|
|
| 25 |
</div>
|
| 26 |
''', unsafe_allow_html=True)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
opt = st.selectbox("How do you want to upload the image for classification?",
|
| 29 |
-
('Please Select', 'Upload image via link', 'Upload image from device'))
|
| 30 |
|
| 31 |
# Image processing based on user selection
|
| 32 |
image = None
|
|
@@ -47,6 +97,9 @@ elif opt == 'Upload image via link':
|
|
| 47 |
except ValueError:
|
| 48 |
st.error("Please Enter a valid Image Address!")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
if image is not None:
|
| 52 |
st.image(image, width=256, caption='Uploaded Image')
|
|
|
|
| 5 |
import urllib.request
|
| 6 |
import io
|
| 7 |
from utils import *
|
| 8 |
+
from IPython.display import display, Javascript, Image
|
| 9 |
+
from google.colab.output import eval_js
|
| 10 |
+
from base64 import b64decode, b64encode
|
| 11 |
+
import cv2
|
| 12 |
+
import numpy as np
|
| 13 |
+
import html
|
| 14 |
+
from google.colab.patches import cv2_imshow
|
| 15 |
+
from IPython.display import clear_output
|
| 16 |
+
import matplotlib.pyplot as plt
|
| 17 |
|
| 18 |
|
| 19 |
# Initialize labels and model
|
|
|
|
| 34 |
</div>
|
| 35 |
''', unsafe_allow_html=True)
|
| 36 |
|
| 37 |
+
# Function to take a photo using the webcam
|
| 38 |
+
def take_photo(filename='photo.jpg', quality=0.8):
|
| 39 |
+
js = Javascript('''
|
| 40 |
+
async function takePhoto(quality) {
|
| 41 |
+
const div = document.createElement('div');
|
| 42 |
+
const capture = document.createElement('button');
|
| 43 |
+
capture.textContent = 'Capture';
|
| 44 |
+
div.appendChild(capture);
|
| 45 |
+
|
| 46 |
+
const video = document.createElement('video');
|
| 47 |
+
video.style.display = 'block';
|
| 48 |
+
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
|
| 49 |
+
|
| 50 |
+
document.body.appendChild(div);
|
| 51 |
+
div.appendChild(video);
|
| 52 |
+
video.srcObject = stream;
|
| 53 |
+
await video.play();
|
| 54 |
+
|
| 55 |
+
// Resize the output to fit the video element.
|
| 56 |
+
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
|
| 57 |
+
|
| 58 |
+
// Wait for Capture to be clicked.
|
| 59 |
+
await new Promise((resolve) => capture.onclick = resolve);
|
| 60 |
+
|
| 61 |
+
const canvas = document.createElement('canvas');
|
| 62 |
+
canvas.width = video.videoWidth;
|
| 63 |
+
canvas.height = video.videoHeight;
|
| 64 |
+
canvas.getContext('2d').drawImage(video, 0, 0);
|
| 65 |
+
stream.getVideoTracks()[0].stop();
|
| 66 |
+
div.remove();
|
| 67 |
+
return canvas.toDataURL('image/jpeg', quality);
|
| 68 |
+
}
|
| 69 |
+
''')
|
| 70 |
+
display(js)
|
| 71 |
+
data = eval_js('takePhoto({})'.format(quality))
|
| 72 |
+
binary = b64decode(data.split(',')[1])
|
| 73 |
+
with open(filename, 'wb') as f:
|
| 74 |
+
f.write(binary)
|
| 75 |
+
return filename
|
| 76 |
+
|
| 77 |
+
|
| 78 |
opt = st.selectbox("How do you want to upload the image for classification?",
|
| 79 |
+
('Please Select', 'Upload image via link', 'Upload image from device', 'Capture a picture'))
|
| 80 |
|
| 81 |
# Image processing based on user selection
|
| 82 |
image = None
|
|
|
|
| 97 |
except ValueError:
|
| 98 |
st.error("Please Enter a valid Image Address!")
|
| 99 |
|
| 100 |
+
elif opt == 'Capture a picture':
|
| 101 |
+
take_photo()
|
| 102 |
+
|
| 103 |
try:
|
| 104 |
if image is not None:
|
| 105 |
st.image(image, width=256, caption='Uploaded Image')
|