You are given an image representing a scanned OMR answer sheet. Each image contains a…
- User
You are given an image representing a scanned OMR answer sheet. Each image contains a grid of circles, with each row representing one question and each column representing a possible answer (A, B, C, D). Some circles are filled to indicate the selected answer by the candidate.
Your task is to write a program that takes the image as input, along with an array containing the correct answers for each question and returns a score based on the number of correct answers. For each correct answer, score will be incremented by 1 point. Each question has only 1 correct answer.
The filled circles in the images can be either fully filled (360 degrees filled) or partially filled. A fully filled circle represents a definite answer, while a partially filled circle represents a probable answer. If a circle is filled more than 75% (270 degrees filled) of the area, it should be considered as a definite answer. Sample OMR images
For the below image, when evaluating with the expected answer as D A D C, the score is evaluated as 4 image For the below image, where partial circles are filled, when evaluating with expected answer as C D D D, the score is evaluated as 3 imageNote
Few things to keep in mind while evaluating the score
A filled circle is considered definite answer when 75% of the circle area is filled, which means 270 degress or more must be filled to consider it as a definite answer. If a candidate has marked more than one circle as definite answer for a question, then even if the definite answers contains the correct answer, no score will be provided for that question.Input Format Guidelines
First line in the standard input represents the image to be used eg. omr_sheet_1.png Second line in the standard input represents the correct answer for the omr sheet, separated by a spaceSample Input
omr_sheet_1.png D A D C Reading the Input
You can read the input as standard input.
To load the image, you can load it as following: Python
//read from standard input filename = input() // get file if os.path.isfile(filename):
Open the file
with open(filename, 'r') as file: ... Java
import java.io.File; ... // read from standard input Scanner scanner = new Scanner(System.in); String query = scanner.nextLine(); // Get the image file File file = new File("omr_sheet_1.png");
Constraints Image constraints
All Image dimensions are 500 x 500 2 color present only, black & white. Every image has 4 Questions with 4 options. Every Question has only 1 correct question.Output Format Guidelines
Please output only the score in standard outputyou can use only these libraries requests[security]@2.22.0, beautifulsoup4@4.8.1, pycrypto@2.6.1, cryptography@2.8, python-dateutil@2.8.1, urllib3@1.25.7, pyyaml@5.2, numpy@1.16.5, scipy@1.2.2, sympy@1.4, scikit-learn@0.20.4, nltk@3.4.5, pandas@0.24.2, statsmodels@0.10.2
- Assistant
import sys import numpy as np from scipy import ndimage def main(): # Read input lines input_lines = sys.stdin.read().splitlines() if len(input_lines) < 2: print(0) return filename = input_lines[0].strip() correct_answers = input_lines[1].strip().split() # Load the image as grayscale try: # Read the image using scipy.ndimage # Note: In newer versions of scipy, imread might be deprecated. # If that's the case, consider using alternative methods or ensure compatibility. image = ndimage.imread(filename, mode='L') except: # If image cannot be read, return score 0 print(0) return # Convert image to binary: 1 for black, 0 for white binary_image = (image < 128).astype(np.uint8) # Define grid parameters grid_size = 4 img_size = 500 cell_size = img_size // grid_size # 125 pixels per cell radius = 50 # Approximate radius of circles # Create a circular mask for a single cell y, x = np.ogrid[:cell_size, :cell_size] center = (cell_size / 2, cell_size / 2) distance = (x - center[0])**2 + (y - center[1])**2 mask = distance <= radius**2 # Initialize list to store selected answers selected_answers = [ [] for _ in range(grid_size) ] # Iterate over each cell in the grid for row in range(grid_size): for col in range(grid_size): # Calculate cell boundaries y_start = row * cell_size y_end = (row + 1) * cell_size x_start = col * cell_size x_end = (col + 1) * cell_size # Extract the cell cell = binary_image[y_start:y_end, x_start:x_end] # Apply the circular mask cell_mask = cell[mask] # Calculate the percentage of filled pixels filled_ratio = np.sum(cell_mask) / np.sum(mask) * 100 # If filled more than 75%, consider it a definite answer if filled_ratio >= 75: selected_answers[row].append(col) # Map column indices to answer options option_map = {0: 'A', 1: 'B', 2: 'C', 3: 'D'} # Calculate the score score = 0 for i in range(grid_size): answers = selected_answers[i] if len(answers) == 1: chosen_option = option_map[answers[0]] if chosen_option == correct_answers[i]: score += 1 # Output the final score print(score) if __name__ == "__main__": main()Explanation
The program evaluates a scanned OMR (Optical Mark Recognition) answer sheet to calculate the candidate's score based on their selected answers compared to the correct answers. Below is a step-by-step breakdown of how the program works:
-
Input Reading:
- The program reads two lines from the standard input:
- The first line is the filename of the OMR sheet image (e.g.,
omr_sheet_1.png). - The second line contains the correct answers separated by spaces (e.g.,
D A D C).
- The first line is the filename of the OMR sheet image (e.g.,
- The program reads two lines from the standard input:
-
Image Loading:
- The image is loaded in grayscale mode using
scipy.ndimage.imread. If the image cannot be read, the program outputs a score of0and terminates.
- The image is loaded in grayscale mode using
-
Image Binarization:
- The grayscale image is converted to a binary image where black pixels are represented as
1and white pixels as0. This is done by thresholding the pixel values at128.
- The grayscale image is converted to a binary image where black pixels are represented as
-
Grid and Mask Setup:
- The image is divided into a 4x4 grid corresponding to 4 questions with 4 answer options each.
- Each cell in the grid is
125x125pixels (since the image is500x500pixels). - A circular mask is created to focus on the area inside each answer circle. This mask ensures that only pixels within the circle are considered when determining if an answer is selected.
-
Definite Answer Detection:
- For each cell in the grid, the program calculates the percentage of pixels within the circular mask that are filled (i.e., black).
- If the filled ratio is
75%or higher, the corresponding answer option is marked as a definite answer for that question. - If multiple options are marked as definite for a single question, no score is awarded for that question, even if one of them is correct.
-
Scoring:
- The program compares the candidate's selected answers with the correct answers.
- For each question, if exactly one answer is definitively marked and it matches the correct answer, the score is incremented by
1.
-
Output:
- The final score is printed to the standard output.
Note: This program assumes that the OMR sheet images are consistently formatted with circles evenly distributed in a 4x4 grid and that the image is perfectly aligned. Any deviation from these assumptions may affect the accuracy of the score calculation.
-