bokharim24 commited on
Commit
67216ee
·
1 Parent(s): ea488fc

Image recognition

Browse files
captured_image.jpg CHANGED
captured_image_1.jpg ADDED
captured_images/captured_image_1.jpg ADDED
captured_images/captured_image_10.jpg ADDED
captured_images/captured_image_11.jpg ADDED
captured_images/captured_image_12.jpg ADDED
captured_images/captured_image_13.jpg ADDED
captured_images/captured_image_2.jpg ADDED
captured_images/captured_image_3.jpg ADDED
captured_images/captured_image_4.jpg ADDED
captured_images/captured_image_5.jpg ADDED
captured_images/captured_image_6.jpg ADDED
captured_images/captured_image_7.jpg ADDED
captured_images/captured_image_8.jpg ADDED
captured_images/captured_image_9.jpg ADDED
index.py CHANGED
@@ -2,67 +2,85 @@ import streamlit as st
2
  import cv2
3
  import os
4
 
 
 
 
 
5
  def main():
6
- st.title('NouriScan')
7
 
8
  st.sidebar.header('Ingredients & Nutrition')
9
 
 
10
  option1 = st.sidebar.checkbox('Banana')
11
  option2 = st.sidebar.checkbox('Strawberry')
12
  option3 = st.sidebar.checkbox('Kale')
13
  option4 = st.sidebar.checkbox('Orange Juice')
14
  option5 = st.sidebar.checkbox('Almond Milk')
15
-
16
  button_clicked = st.sidebar.button('Done')
17
-
18
- if not button_clicked:
19
- videoCapture()
20
- else:
21
  displayRecipes()
22
 
23
- def displayRecipes():
24
- items = [
25
- {"title": "Item 1", "content": "Content for Item 1."},
26
- {"title": "Item 2", "content": "Content for Item 2."},
27
- {"title": "Item 3", "content": "Content for Item 3."}
28
- ]
29
-
30
- for item in items:
31
- with st.expander(item["title"]):
32
- st.write(item["content"])
33
 
34
- def videoCapture():
35
  cap = cv2.VideoCapture(0)
 
 
36
  cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
37
  cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
38
 
 
39
  if not cap.isOpened():
40
  st.error("Error: Unable to access the webcam.")
41
  return
42
 
43
- st.write("Click 'Capture Image' to capture an image.")
44
-
45
  # Display a placeholder for the video stream
46
  video_placeholder = st.empty()
47
 
 
48
  if st.button("Capture Image"):
 
 
 
49
  # Read a frame from the webcam
50
  ret, frame = cap.read()
51
 
52
  if not ret:
53
  st.error("Error: Unable to read frame from the webcam.")
54
- else:
55
- # Save the captured frame as an image
56
- image_path = "captured_image.jpg"
57
- cv2.imwrite(image_path, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
58
- st.write("Image captured and saved as", image_path)
59
 
60
- # Display the captured image
61
- st.image(image_path, use_column_width=True)
62
 
63
  # Release the VideoCapture and close the OpenCV window
64
  cap.release()
65
- cv2.destroyAllWindows()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  if __name__ == '__main__':
68
  main()
 
2
  import cv2
3
  import os
4
 
5
+ # Create a folder to save captured images
6
+ if not os.path.exists("captured_images"):
7
+ os.makedirs("captured_images")
8
+
9
  def main():
10
+ st.title('Image Capture App')
11
 
12
  st.sidebar.header('Ingredients & Nutrition')
13
 
14
+
15
  option1 = st.sidebar.checkbox('Banana')
16
  option2 = st.sidebar.checkbox('Strawberry')
17
  option3 = st.sidebar.checkbox('Kale')
18
  option4 = st.sidebar.checkbox('Orange Juice')
19
  option5 = st.sidebar.checkbox('Almond Milk')
 
20
  button_clicked = st.sidebar.button('Done')
21
+ if button_clicked:
 
 
 
22
  displayRecipes()
23
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ # Create a VideoCapture object to access the webcam
26
  cap = cv2.VideoCapture(0)
27
+
28
+ # Set the video frame width and height (optional)
29
  cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
30
  cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
31
 
32
+ # Check if the webcam is opened correctly
33
  if not cap.isOpened():
34
  st.error("Error: Unable to access the webcam.")
35
  return
36
 
 
 
37
  # Display a placeholder for the video stream
38
  video_placeholder = st.empty()
39
 
40
+ # Button to capture image
41
  if st.button("Capture Image"):
42
+ capture_image(cap)
43
+
44
+ while True:
45
  # Read a frame from the webcam
46
  ret, frame = cap.read()
47
 
48
  if not ret:
49
  st.error("Error: Unable to read frame from the webcam.")
50
+ break
 
 
 
 
51
 
52
+ # Display the frame in the Streamlit app
53
+ video_placeholder.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), channels="RGB", use_column_width=True)
54
 
55
  # Release the VideoCapture and close the OpenCV window
56
  cap.release()
57
+
58
+ def displayRecipes():
59
+ items = [
60
+ {"title": "Item 1", "content": "Content for Item 1."},
61
+ {"title": "Item 2", "content": "Content for Item 2."},
62
+ {"title": "Item 3", "content": "Content for Item 3."}
63
+ ]
64
+
65
+ # Display the items with expanding boxes
66
+ for item in items:
67
+ with st.expander(item["title"]):
68
+ st.write(item["content"])
69
+
70
+
71
+
72
+ def capture_image(cap):
73
+ # Read a frame from the webcam
74
+ ret, frame = cap.read()
75
+
76
+ if not ret:
77
+ st.error("Error: Unable to read frame from the webcam.")
78
+ return
79
+
80
+ # Save the frame as an image
81
+ image_path = f"captured_images/captured_image_{len(os.listdir('captured_images')) + 1}.jpg"
82
+ cv2.imwrite(image_path, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
83
+ st.success(f"Image captured and saved as {image_path}")
84
 
85
  if __name__ == '__main__':
86
  main()