KhaqanNasir commited on
Commit
7d734fc
·
verified ·
1 Parent(s): 5133c77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -67
app.py CHANGED
@@ -1,67 +1,73 @@
1
- import cv2
2
- import pytesseract
3
- from PIL import Image
4
- import streamlit as st
5
- from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
6
- import numpy as np
7
- import os
8
-
9
- # Set the Tesseract path for Hugging Face Spaces (assuming Tesseract is installed)
10
- pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
11
-
12
- # Streamlit UI
13
- st.title("Quiz Checker App")
14
- st.header("Upload an Answer Key and Use Webcam for Real-Time Evaluation")
15
-
16
- # Upload answer key
17
- uploaded_file = st.file_uploader("Upload Answer Key Image (JPG/PNG)", type=["jpg", "jpeg", "png"])
18
-
19
- if uploaded_file:
20
- # Open the uploaded image
21
- answer_key_img = Image.open(uploaded_file)
22
-
23
- # Ensure the image is in RGB format
24
- answer_key_img = answer_key_img.convert("RGB")
25
-
26
- # Use Tesseract to extract text from the image
27
- answer_key_text = pytesseract.image_to_string(answer_key_img)
28
-
29
- # Split and clean extracted text
30
- answers = [ans.strip() for ans in answer_key_text.splitlines() if ans.strip()]
31
-
32
- st.success("Answer key loaded successfully!")
33
- st.write("Extracted Answers:")
34
- st.write(answers)
35
-
36
- class QuizChecker(VideoTransformerBase):
37
- def transform(self, frame):
38
- frame = frame.to_ndarray(format="bgr24")
39
-
40
- # Convert frame to grayscale
41
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
42
-
43
- # Use Tesseract to extract text from the frame
44
- student_text = pytesseract.image_to_string(gray)
45
- student_answers = [ans.strip() for ans in student_text.splitlines() if ans.strip()]
46
-
47
- # Overlay results on the video feed
48
- correct_count = 0
49
- for idx, student_answer in enumerate(student_answers):
50
- if idx < len(answers) and student_answer == answers[idx]:
51
- color = (0, 255, 0) # Green for correct
52
- correct_count += 1
53
- else:
54
- color = (0, 0, 255) # Red for incorrect
55
- cv2.putText(frame, f"Q{idx+1}: {student_answer}", (10, 50 + idx * 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
56
-
57
- # Display score
58
- cv2.putText(frame, f"Score: {correct_count}/{len(answers)}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
59
-
60
- return frame
61
-
62
- # Streamlit UI for starting webcam
63
- st.header("Start Webcam")
64
- webrtc_streamer(key="quiz-checker", video_transformer_factory=QuizChecker, media_stream_constraints={"video": True})
65
-
66
- else:
67
- st.info("Please upload an answer key image to proceed.")
 
 
 
 
 
 
 
1
+ import cv2
2
+ import pytesseract
3
+ from PIL import Image
4
+ import streamlit as st
5
+ from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
6
+ import numpy as np
7
+ import os
8
+
9
+ # # Set the Tesseract path for Hugging Face Spaces (assuming Tesseract is installed)
10
+ # pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract'
11
+
12
+ # Print the Tesseract path to verify its location
13
+ tesseract_path = pytesseract.pytesseract.tesseract_cmd
14
+
15
+ # Display the path in the Streamlit app
16
+ st.write(f"Tesseract path: {tesseract_path}")
17
+
18
+ # Streamlit UI
19
+ st.title("Quiz Checker App")
20
+ st.header("Upload an Answer Key and Use Webcam for Real-Time Evaluation")
21
+
22
+ # Upload answer key
23
+ uploaded_file = st.file_uploader("Upload Answer Key Image (JPG/PNG)", type=["jpg", "jpeg", "png"])
24
+
25
+ if uploaded_file:
26
+ # Open the uploaded image
27
+ answer_key_img = Image.open(uploaded_file)
28
+
29
+ # Ensure the image is in RGB format
30
+ answer_key_img = answer_key_img.convert("RGB")
31
+
32
+ # Use Tesseract to extract text from the image
33
+ answer_key_text = pytesseract.image_to_string(answer_key_img)
34
+
35
+ # Split and clean extracted text
36
+ answers = [ans.strip() for ans in answer_key_text.splitlines() if ans.strip()]
37
+
38
+ st.success("Answer key loaded successfully!")
39
+ st.write("Extracted Answers:")
40
+ st.write(answers)
41
+
42
+ class QuizChecker(VideoTransformerBase):
43
+ def transform(self, frame):
44
+ frame = frame.to_ndarray(format="bgr24")
45
+
46
+ # Convert frame to grayscale
47
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
48
+
49
+ # Use Tesseract to extract text from the frame
50
+ student_text = pytesseract.image_to_string(gray)
51
+ student_answers = [ans.strip() for ans in student_text.splitlines() if ans.strip()]
52
+
53
+ # Overlay results on the video feed
54
+ correct_count = 0
55
+ for idx, student_answer in enumerate(student_answers):
56
+ if idx < len(answers) and student_answer == answers[idx]:
57
+ color = (0, 255, 0) # Green for correct
58
+ correct_count += 1
59
+ else:
60
+ color = (0, 0, 255) # Red for incorrect
61
+ cv2.putText(frame, f"Q{idx+1}: {student_answer}", (10, 50 + idx * 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2)
62
+
63
+ # Display score
64
+ cv2.putText(frame, f"Score: {correct_count}/{len(answers)}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
65
+
66
+ return frame
67
+
68
+ # Streamlit UI for starting webcam
69
+ st.header("Start Webcam")
70
+ webrtc_streamer(key="quiz-checker", video_transformer_factory=QuizChecker, media_stream_constraints={"video": True})
71
+
72
+ else:
73
+ st.info("Please upload an answer key image to proceed.")