Spaces:
Running
Running
Commit
·
3d1213b
1
Parent(s):
1038417
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,82 +26,30 @@ st.markdown('''
|
|
| 26 |
</div>
|
| 27 |
''', unsafe_allow_html=True)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
def take_photo(filename='photo.jpg', quality=
|
| 31 |
-
0.8):
|
| 32 |
-
js = Javascript('''
|
| 33 |
-
async function takePhoto(quality) {
|
| 34 |
-
const div = document.createElement('div');
|
| 35 |
-
const capture = document.createElement('button');
|
| 36 |
-
capture.textContent = 'Capture';
|
| 37 |
-
div.appendChild(capture);
|
| 38 |
-
|
| 39 |
-
const video = document.createElement('video');
|
| 40 |
-
video.style.display = 'block';
|
| 41 |
-
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
|
| 42 |
-
|
| 43 |
-
document.body.appendChild(div);
|
| 44 |
-
div.appendChild(video);
|
| 45 |
-
video.srcObject = stream;
|
| 46 |
-
await video.play();
|
| 47 |
-
|
| 48 |
-
// Resize the output to fit the video element.
|
| 49 |
-
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
|
| 50 |
-
|
| 51 |
-
// Wait for Capture to be clicked.
|
| 52 |
-
await new Promise((resolve) => capture.onclick = resolve);
|
| 53 |
-
|
| 54 |
-
const canvas = document.createElement('canvas');
|
| 55 |
-
canvas.width = video.videoWidth;
|
| 56 |
-
canvas.height = video.videoHeight;
|
| 57 |
-
canvas.getContext('2d').drawImage(video, 0, 0);
|
| 58 |
-
stream.getVideoTracks()[0].stop();
|
| 59 |
-
div.remove();
|
| 60 |
-
return canvas.toDataURL('image/jpeg', quality);
|
| 61 |
-
}
|
| 62 |
-
''')
|
| 63 |
-
display(js)
|
| 64 |
-
data = eval_js('takePhoto({})'.format(quality))
|
| 65 |
-
binary = b64decode(data.split(',')[1])
|
| 66 |
-
with open(filename, 'wb') as f:
|
| 67 |
-
f.write(binary)
|
| 68 |
-
return filename
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
opt = st.selectbox("How do you want to upload the image for classification?",
|
| 72 |
-
('Please Select', 'Upload image via link', 'Upload image from device', 'Capture a picture'))
|
| 73 |
-
|
| 74 |
-
# Image processing based on user selection
|
| 75 |
image = None
|
| 76 |
if opt == 'Upload image from device':
|
| 77 |
file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
|
| 78 |
if file:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
image = cv2.resize(image, (256, 256))
|
| 82 |
-
except Exception as e:
|
| 83 |
-
st.error(f"Error reading the file: {e}")
|
| 84 |
|
| 85 |
elif opt == 'Upload image via link':
|
| 86 |
img_url = st.text_input('Enter the Image Address')
|
| 87 |
if st.button('Submit'):
|
| 88 |
try:
|
| 89 |
response = urllib.request.urlopen(img_url)
|
| 90 |
-
image =
|
| 91 |
-
image = cv2.resize(image, (256, 256))
|
| 92 |
except ValueError:
|
| 93 |
st.error("Please Enter a valid Image Address!")
|
| 94 |
|
| 95 |
-
|
| 96 |
-
take_photo()
|
| 97 |
|
| 98 |
try:
|
| 99 |
if image is not None:
|
| 100 |
st.image(image, width=256, caption='Uploaded Image')
|
| 101 |
if st.button('Predict'):
|
| 102 |
-
|
| 103 |
-
#model = model_arc()
|
| 104 |
-
# model.load_weights("classify_model.h5")
|
| 105 |
|
| 106 |
print("---------------img-array---------------------")
|
| 107 |
print(img[np.newaxis, ...])
|
|
|
|
| 26 |
</div>
|
| 27 |
''', unsafe_allow_html=True)
|
| 28 |
|
| 29 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
image = None
|
| 31 |
if opt == 'Upload image from device':
|
| 32 |
file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
|
| 33 |
if file:
|
| 34 |
+
image = preprocess_image(file)
|
| 35 |
+
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
elif opt == 'Upload image via link':
|
| 38 |
img_url = st.text_input('Enter the Image Address')
|
| 39 |
if st.button('Submit'):
|
| 40 |
try:
|
| 41 |
response = urllib.request.urlopen(img_url)
|
| 42 |
+
image = preprocess_image(response)
|
|
|
|
| 43 |
except ValueError:
|
| 44 |
st.error("Please Enter a valid Image Address!")
|
| 45 |
|
| 46 |
+
|
|
|
|
| 47 |
|
| 48 |
try:
|
| 49 |
if image is not None:
|
| 50 |
st.image(image, width=256, caption='Uploaded Image')
|
| 51 |
if st.button('Predict'):
|
| 52 |
+
|
|
|
|
|
|
|
| 53 |
|
| 54 |
print("---------------img-array---------------------")
|
| 55 |
print(img[np.newaxis, ...])
|