File size: 11,240 Bytes
84f2275
 
 
 
 
 
 
 
 
7f269b9
84f2275
 
 
7f269b9
84f2275
 
7f269b9
84f2275
 
 
 
 
 
b8bfa11
84f2275
 
b8bfa11
84f2275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8bfa11
84f2275
7fa9057
84f2275
 
7fa9057
84f2275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f269b9
84f2275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa9057
84f2275
7fa9057
84f2275
7fa9057
84f2275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fa9057
84f2275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f269b9
 
7fa9057
84f2275
7fa9057
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
from fastapi import FastAPI, UploadFile, File, HTTPException, Query
from fastapi.responses import JSONResponse, StreamingResponse
import uvicorn
import io
import json
import numpy as np
import cv2
from PIL import Image
from pdf2image import convert_from_bytes

# Import the Google GenAI client libraries.
from google import genai
from google.genai import types

# Initialize the GenAI client with your API key.
client = genai.Client(api_key="AIzaSyDDDHg9GWl6-9aq9Wo43GHfk2wcakhgwBQ")

app = FastAPI(title="Student Result Card API")

# -----------------------------
# Preprocessing Methods
# -----------------------------
def preprocess_candidate_info(image_cv):
    """
    Preprocess the image to extract the candidate information region.
    Region is defined by a mask covering the top-left portion.
    """
    height, width = image_cv.shape[:2]
    mask = np.zeros((height, width), dtype="uint8")
    margin_top = int(height * 0.10)
    margin_bottom = int(height * 0.25)
    cv2.rectangle(mask, (0, margin_top), (width, height - margin_bottom), 255, -1)
    masked = cv2.bitwise_and(image_cv, image_cv, mask=mask)
    coords = cv2.findNonZero(mask)
    x, y, w, h = cv2.boundingRect(coords)
    cropped = masked[y:y+h, x:x+w]
    return Image.fromarray(cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB))

def preprocess_mcq(image_cv):
    """
    Preprocess the image to extract the MCQ answers region (questions 1 to 10).
    Region is defined by a mask on the left side of the page.
    """
    height, width = image_cv.shape[:2]
    mask = np.zeros((height, width), dtype="uint8")
    margin_top = int(height * 0.27)
    margin_bottom = int(height * 0.23)
    right_boundary = int(width * 0.35)
    cv2.rectangle(mask, (0, margin_top), (right_boundary, height - margin_bottom), 255, -1)
    masked = cv2.bitwise_and(image_cv, image_cv, mask=mask)
    coords = cv2.findNonZero(mask)
    x, y, w, h = cv2.boundingRect(coords)
    cropped = masked[y:y+h, x:x+w]
    return Image.fromarray(cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB))

def preprocess_free_response(image_cv):
    """
    Preprocess the image to extract the free-response answers region (questions 11 to 15).
    Region is defined by a mask on the middle-right part of the page.
    """
    height, width = image_cv.shape[:2]
    mask = np.zeros((height, width), dtype="uint8")
    margin_top = int(height * 0.27)
    margin_bottom = int(height * 0.38)
    left_boundary = int(width * 0.35)
    right_boundary = int(width * 0.68)
    cv2.rectangle(mask, (left_boundary, margin_top), (right_boundary, height - margin_bottom), 255, -1)
    masked = cv2.bitwise_and(image_cv, image_cv, mask=mask)
    coords = cv2.findNonZero(mask)
    x, y, w, h = cv2.boundingRect(coords)
    cropped = masked[y:y+h, x:x+w]
    return Image.fromarray(cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB))

def preprocess_full_answers(image_cv):
    """
    For extracting the correct answer key, we assume the entire page contains the answers.
    """
    return Image.fromarray(cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB))

# -----------------------------
# Extraction Methods using Gemini
# -----------------------------
def extract_json_from_output(output_str):
    """
    Extracts a JSON object from a string containing extra text.
    """
    start = output_str.find('{')
    end = output_str.rfind('}')
    if start == -1 or end == -1:
        return None
    json_str = output_str[start:end+1]
    try:
        return json.loads(json_str)
    except json.JSONDecodeError:
        return None

def get_student_info(image_input):
    """
    Extracts candidate information from an image.
    """
    output_format = """
Answer in the following JSON format. Do not write anything else:
{
  "Candidate Info": {
    "Name": "<name>",
    "Number": "<number>",
    "Country": "<country>",
    "Level": "<level>"
  }
}
"""
    prompt = f"""
You are an assistant that extracts candidate information from an image.
The image contains details including name, candidate number, country, and level.
Extract the information accurately and provide the result in JSON using the format below:
{output_format}
"""
    response = client.models.generate_content(model="gemini-2.0-flash", contents=[prompt, image_input])
    return extract_json_from_output(response.text)

def get_mcq_answers(image_input):
    """
    Extracts multiple-choice answers (questions 1 to 10) from an image.
    """
    output_format = """
Answer in the following JSON format do not write anything else:
{
  "Answers": {
    "1": "<option>",
    "2": "<option>",
    "3": "<option>",
    "4": "<option>",
    "5": "<option>",
    "6": "<option>",
    "7": "<option>",
    "8": "<option>",
    "9": "<option>",
    "10": "<option>"
  }
}
"""
    prompt = f"""
You are an assistant that extracts MCQ answers from an image.
The image is a screenshot of a 10-question multiple-choice answer sheet.
Extract which option is marked for each question (1 to 10) and provide the answers in JSON using the format below:
{output_format}
"""
    response = client.models.generate_content(model="gemini-2.0-flash", contents=[prompt, image_input])
    return extract_json_from_output(response.text)

def get_free_response_answers(image_input):
    """
    Extracts free-text answers (questions 11 to 15) from an image.
    """
    output_format = """
Answer in the following JSON format. Do not write anything else:
{
  "Free Answers": {
    "11": "<answer for question 11>",
    "12": "<answer for question 12>",
    "13": "<answer for question 13>",
    "14": "<answer for question 14>",
    "15": "<answer for question 15>"
  }
}
"""
    prompt = f"""
You are an assistant that extracts free-text answers from an image.
The image contains responses for questions 11 to 15.
Extract the answers accurately and provide the result in JSON using the format below:
{output_format}
"""
    response = client.models.generate_content(model="gemini-2.0-flash", contents=[prompt, image_input])
    return extract_json_from_output(response.text)

def get_all_answers(image_input):
    """
    Extracts all answers (questions 1 to 15) from an image of the correct answer key.
    """
    output_format = """
Answer in the following JSON format. Do not write anything else:
{
  "Answers": {
    "1": "<option or text>",
    "2": "<option or text>",
    "3": "<option or text>",
    "4": "<option or text>",
    "5": "<option or text>",
    "6": "<option or text>",
    "7": "<option or text>",
    "8": "<option or text>",
    "9": "<option or text>",
    "10": "<option or text>",
    "11": "<free-text answer>",
    "12": "<free-text answer>",
    "13": "<free-text answer>",
    "14": "<free-text answer>",
    "15": "<free-text answer>"
  }
}
"""
    prompt = f"""
You are an assistant that extracts answers from an image.
The image is a screenshot of an answer sheet containing 15 questions.
For questions 1 to 10, the answers are multiple-choice selections.
For questions 11 to 15, the answers are free-text responses.
Extract the answer for each question and provide the result in JSON using the format below:
{output_format}
"""
    response = client.models.generate_content(model="gemini-2.0-flash", contents=[prompt, image_input])
    return extract_json_from_output(response.text)

# -----------------------------
# Method to calculate result card
# -----------------------------
def calculate_result(student_info, student_mcq, student_free, correct_answers):
    """
    Compares student's answers with the correct answers, calculates marks and percentage,
    and returns a result card in JSON.
    """
    student_all = {}
    if student_mcq and "Answers" in student_mcq:
        student_all.update(student_mcq["Answers"])
    if student_free and "Free Answers" in student_free:
        student_all.update(student_free["Free Answers"])
    
    correct_all = correct_answers.get("Answers", {})
    total_questions = 15
    marks = 0
    detailed = {}
    
    for q in map(str, range(1, total_questions + 1)):
        student_ans = student_all.get(q, "").strip()
        correct_ans = correct_all.get(q, "").strip()
        if student_ans == correct_ans:
            marks += 1
            detailed[q] = {"Student": student_ans, "Correct": correct_ans, "Result": "Correct"}
        else:
            detailed[q] = {"Student": student_ans, "Correct": correct_ans, "Result": "Incorrect"}
    
    percentage = (marks / total_questions) * 100
    result_card = {
        "Candidate Info": student_info.get("Candidate Info", {}),
        "Total Marks": marks,
        "Total Questions": total_questions,
        "Percentage": percentage,
        "Detailed Results": detailed
    }
    return result_card

# -----------------------------
# API Endpoint to process PDFs and return student result cards
# -----------------------------
@app.post("/process")
async def process_pdfs(
    student_pdf: UploadFile = File(...),
    answer_key_pdf: UploadFile = File(...),
    download: bool = Query(False, description="Set to true to download result card list as a JSON file")
):
    try:
        # Read student PDF bytes and convert to images
        student_bytes = await student_pdf.read()
        student_images = convert_from_bytes(student_bytes)
        
        # Read answer key PDF bytes and convert to images; assume correct key is in the last page.
        answer_key_bytes = await answer_key_pdf.read()
        answer_key_images = convert_from_bytes(answer_key_bytes)
        last_page = answer_key_images[-1]
        last_page_cv = np.array(last_page)
        last_page_cv = cv2.cvtColor(last_page_cv, cv2.COLOR_RGB2BGR)
        correct_image = preprocess_full_answers(last_page_cv)
        correct_answers = get_all_answers(correct_image)
        
        student_result_cards = []
        
        # Process each student page.
        for idx, page in enumerate(student_images):
            page_cv = np.array(page)
            page_cv = cv2.cvtColor(page_cv, cv2.COLOR_RGB2BGR)
            student_info_image = preprocess_candidate_info(page_cv)
            mcq_image = preprocess_mcq(page_cv)
            free_image = preprocess_free_response(page_cv)
            
            student_info = get_student_info(student_info_image)
            student_mcq  = get_mcq_answers(mcq_image)
            student_free = get_free_response_answers(free_image)
            
            result_card = calculate_result(student_info, student_mcq, student_free, correct_answers)
            result_card["Student Index"] = idx + 1
            student_result_cards.append(result_card)
        
        if download:
            # Create downloadable JSON file
            json_bytes = json.dumps({"result_cards": student_result_cards}, indent=2).encode("utf-8")
            return StreamingResponse(
                io.BytesIO(json_bytes),
                media_type="application/json",
                headers={"Content-Disposition": "attachment; filename=result_cards.json"}
            )
        else:
            return JSONResponse(content={"result_cards": student_result_cards})
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)