SuganthKrishna2003 commited on
Commit
65809e2
·
1 Parent(s): 907ffb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -56
app.py CHANGED
@@ -2,7 +2,6 @@
2
 
3
  from transformers import AutoImageProcessor, AutoModelForImageClassification
4
  from IPython.display import display, Javascript
5
- from google.colab.output import eval_js
6
  from base64 import b64decode
7
  from IPython.display import Image
8
  import cv2
@@ -12,59 +11,36 @@ import time
12
  from transformers import BarkModel, BarkProcessor
13
  from IPython.display import Audio
14
 
15
- # Defining the camera in the system
16
-
17
- def take_photo(filename='photo.jpg', quality=0.8):
18
- js = Javascript('''
19
- async function takePhoto(quality) {
20
- const div = document.createElement('div');
21
- const capture = document.createElement('button');
22
- capture.textContent = 'Capture';
23
- div.appendChild(capture);
24
-
25
- const video = document.createElement('video');
26
- video.style.display = 'block';
27
- const stream = await navigator.mediaDevices.getUserMedia({video: true});
28
-
29
- document.body.appendChild(div);
30
- div.appendChild(video);
31
- video.srcObject = stream;
32
- await video.play();
33
-
34
- // Resize the output to fit the video element.
35
- google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
36
-
37
- // Wait for Capture to be clicked.
38
- await new Promise((resolve) => capture.onclick = resolve);
39
-
40
- const canvas = document.createElement('canvas');
41
- canvas.width = video.videoWidth;
42
- canvas.height = video.videoHeight;
43
- canvas.getContext('2d').drawImage(video, 0, 0);
44
- stream.getVideoTracks()[0].stop();
45
- div.remove();
46
- return canvas.toDataURL('image/jpeg', quality);
47
- }
48
- ''')
49
- display(js)
50
- data = eval_js('takePhoto({})'.format(quality))
51
- binary = b64decode(data.split(',')[1])
52
- with open(filename, 'wb') as f:
53
- f.write(binary)
54
- return filename
55
-
56
- # Capturing snaps using given button and saving them
57
-
58
- try:
59
- filename = take_photo()
60
- print('Saved to {}'.format(filename))
61
-
62
- # Show the image which was just taken.
63
- display(Image(filename))
64
- except Exception as err:
65
- # Errors will be thrown if the user does not have a webcam or if they do not
66
- # grant the page permission to access it.
67
- print(str(err))
68
 
69
  # Using the pre-trained Dog Breed Identification Model
70
 
@@ -73,9 +49,9 @@ dog_breed_model = AutoModelForImageClassification.from_pretrained("wesleyacheng/
73
 
74
  # Importing the saved image
75
 
76
- img_path='/content/n02088094_60.jpg'
77
 
78
- image=cv2.imread(img_path)
79
 
80
  # Preprocessing the captured image using pre-trained model based preprocessor
81
 
 
2
 
3
  from transformers import AutoImageProcessor, AutoModelForImageClassification
4
  from IPython.display import display, Javascript
 
5
  from base64 import b64decode
6
  from IPython.display import Image
7
  import cv2
 
11
  from transformers import BarkModel, BarkProcessor
12
  from IPython.display import Audio
13
 
14
+
15
+ # Using captured images
16
+
17
+ def process_webcam_images():
18
+ cap = cv2.VideoCapture(0) # 0 corresponds to the default camera (usually your webcam)
19
+
20
+ while True:
21
+ ret, frame = cap.read()
22
+
23
+ # Send the image to the Hugging Face model for classification
24
+ result = model(frame)
25
+
26
+ # Display the classification result
27
+ print(result)
28
+
29
+ # Display the captured image
30
+ cv2.imshow('Webcam Image', frame)
31
+
32
+ # Press 'q' to quit the loop
33
+ if cv2.waitKey(1) & 0xFF == ord('q'):
34
+ break
35
+
36
+ cap.release()
37
+ cv2.destroyAllWindows()
38
+
39
+ if __name__ == "__main__":
40
+ process_webcam_images()
41
+
42
+ image=process_webcam_images()
43
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # Using the pre-trained Dog Breed Identification Model
46
 
 
49
 
50
  # Importing the saved image
51
 
52
+ #img_path='/content/n02088094_60.jpg'
53
 
54
+ #image=cv2.imread(img_path)
55
 
56
  # Preprocessing the captured image using pre-trained model based preprocessor
57