
    }h                        d dl Z d dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlZ	  ej                  e j                  j                  d             d Zd Zd Zd	 Zy# e$ rZ ede         e        Y dZ[)dZ[ww xY w)
    N)ImageGOOGLE_API_KEY)api_keyzBError: GOOGLE_API_KEY not found. Please set it in your .env file. c                 |   	 t        j                  |       j                  d      }t        j                  |      j                  d      }t        j                          d}t
        j                  j                  ||      }|j                  |d|g       ||fS # t        $ r}t        d|        Y d}~yd}~ww xY w)z&Combines two images into a single PDF.RGBz.pdfT)save_allappend_imageszError combining images to PDF: N)NN)r   openconvertuuiduuid4ospathjoinsave	Exceptionprint)image1_pathimage2_pathoutput_folderimg1img2pdf_filenamepdf_pathes           /root/ocr/utils.pycombine_images_to_pdfr      s    	zz+&..u5zz+&..u5**,t,77<<|<		(T$	@%% /s34s   BB 	B;#B66B;c                    	 t        j                  |       }|j                  dk(  r|j                          y|j	                  d      }|j                         }dt        j                          d}t        j                  j                  ||      }|j                  |       |j                          |S # t        $ r}t        d|        Y d}~yd}~ww xY w)zEConverts the first page of a PDF to a JPG image and returns the path.r   N	pdf_page_.jpgzError converting PDF to image: )fitzr
   
page_countclose	load_page
get_pixmapr   r   r   r   r   r   r   r   )r   r   docpagepiximage_filenameimage_filepathr   s           r   convert_pdf_first_page_to_imager+   !   s    ii!>>QIIK}}Qoo$TZZ\N$7m^D 		 /s34s   4B3 A;B3 3	C<CCc                 0   	 d}d}t         j                  j                  ||      }t        j                  |       }|t	        d|         y|j
                  dd \  }}t         j                  j                  t        j                  |d      ddd      }|j                  |       |j                         }	t        j                  |	d	d	dddf         }
|	d	d	|
df   }|d
kD  rt	        d|d       |	d	d	|
ddf   t        j                  ||||g      z  }|j                  d      \  }}}}d}t        d	||z
        }t        d	||z
        }t        |||z         }t        |||z         }|||||f   }|j                   d	k(  rydt#        j$                          d}t&        j(                  j+                  ||      }t        j,                  ||       t	        d|        |S t	        d|dd       y# t.        $ r}t	        d|        Y d}~yd}~ww xY w)z/Detects a face using a stable OpenCV DNN model.zdeploy.prototxtz(res10_300x300_ssd_iter_140000.caffemodelNz7!!! FACE DETECTION ERROR: Could not read image file at    ),  r.   g      ?)g      Z@g      f@g     ^@r   g333333?z2>>> FACE DETECTION: Found a face with confidence: z.2f      int   face_r    z.>>> FACE DETECTION SUCCESS: Face extracted to zO>>> FACE DETECTION: No faces found with sufficient confidence (Max Confidence: z).z:!!! FACE DETECTION CRITICAL ERROR: An exception occurred: )cv2dnnreadNetFromCaffeimreadr   shapeblobFromImageresizesetInputforwardnpargmaxarrayastypemaxminsizer   r   r   r   r   imwriter   )
image_pathr   prototxt_path
model_pathnetimghwblob
detectionsbest_detection_index
confidenceboxstartXstartYendXendYpaddingface_croppedface_filenameface_filepathr   s                         r   detect_and_extract_facerY   3   s,   ,)?
gg&&}jAjj$;KJ<XY2AAww$$SZZZ%@#%/1FH 	T[[]
!yyAq!QJ)?@1&:A =>
FzRUFVWXQ#71<=!QPQST@VVC+.::e+<(VVT4GFW,-FAv?O8Pvq$.)D#a2H4vd{F4K78L  A%d#DJJL>6MGGLLFMKK|4B=/RS  cdnorcssuvw J1#NOs,   A	G4 D8G4 AG4 #G4 4	H=HHc                    	 t        j                  d      }t        j                  |       }d}|j                  ||gddddd      }t	        d|j
                   d       |j
                  j                         s3t	        d	       |j                  rt	        d
|j                          ddiS |j
                  j                         }|j                  d      r|dd }|j                  d      r|dd }t        j                  |      }|S # t        j                  $ r'}t	        d|        dj
                  dcY d}~S d}~wt        $ r%}t	        d|        dt        |      icY d}~S d}~ww xY w)z2Performs OCR on an image or PDF file using Gemini.zgemini-1.5-flash)r   a  
        Analyze the document. Extract all textual information into a structured JSON object.

        **CRITICAL INSTRUCTIONS:**
        1.  **JSON ONLY:** The entire output must be a single, valid JSON object.
        2.  **NO MARKDOWN:** Do not wrap the JSON in ```json ... ``` or any other markdown formatting.
        3.  **NO EXPLANATIONS:** Do not include any introductory text, summary, or any other explanatory sentences before or after the JSON object. Your entire response must start with `{` and end with `}`.
        4.  Use descriptive keys like "fullName", "dateOfBirth", "idNumber", "address".
        5.  If a field is not present, the value must be null.
        
BLOCK_NONE)HARM_CATEGORY_HARASSMENTHARM_CATEGORY_HATE_SPEECHHARM_CATEGORY_SEXUALLY_EXPLICITHARM_CATEGORY_DANGEROUS_CONTENT)safety_settingsz--- RAW GEMINI RESPONSE ---
z
-------------------------zK!!! GEMINI ERROR: Received an empty response. Likely due to safety filters.zPrompt Feedback from API: errorzAAPI returned an empty response, likely blocked by safety filters.z```jsonr0   Nz```zG!!! JSON DECODE ERROR: Failed to parse Gemini response as JSON. Error: z+Failed to parse non-JSON response from API.)ra   raw_responsez5!!! An unexpected error occurred calling Gemini API: )genaiGenerativeModelupload_filegenerate_contentr   textstripprompt_feedback
startswithendswithjsonloadsJSONDecodeErrorr   str)	file_pathmodeluploaded_filepromptresponsecleaned_textdatar   s           r   call_gemini_ocrrx   d   s|   /!%%&89))y9	 ))]#9E:F@L@LN * 
 	-hmm_<WXY}}""$_`''3H4L4L3MNO`aa  }}**,""9-'+L  '',Lzz,' hWXYWZ[\FYaYfYfgg !EaSIJQ  !s7   B,D /AD E2E;E2E2E-'E2-E2)r   r   rm   r4   numpyr=   google.generativeaigenerativeaird   PILr   r!   	configureenvirongetAttributeErrorr   r   exitr   r+   rY   rx        r   <module>r      s}    	   
  #  EOOBJJNN+;<=$.b1!m  	Nqc
RSFFs   ,A B $A;;B 