Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Upload 6 files
Browse files- README.md +13 -12
 - app.py +102 -0
 - detection.pt +3 -0
 - recognization_id.pt +3 -0
 - recognization_model.pth +3 -0
 - requirements.txt +6 -0
 
    	
        README.md
    CHANGED
    
    | 
         @@ -1,12 +1,13 @@ 
     | 
|
| 1 | 
         
            -
            ---
         
     | 
| 2 | 
         
            -
            title:  
     | 
| 3 | 
         
            -
            emoji:  
     | 
| 4 | 
         
            -
            colorFrom:  
     | 
| 5 | 
         
            -
            colorTo:  
     | 
| 6 | 
         
            -
            sdk: gradio
         
     | 
| 7 | 
         
            -
            sdk_version: 5. 
     | 
| 8 | 
         
            -
            app_file: app.py
         
     | 
| 9 | 
         
            -
            pinned: false
         
     | 
| 10 | 
         
            -
             
     | 
| 11 | 
         
            -
             
     | 
| 12 | 
         
            -
             
     | 
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            ---
         
     | 
| 2 | 
         
            +
            title: IDs Demo
         
     | 
| 3 | 
         
            +
            emoji: 🐢
         
     | 
| 4 | 
         
            +
            colorFrom: yellow
         
     | 
| 5 | 
         
            +
            colorTo: purple
         
     | 
| 6 | 
         
            +
            sdk: gradio
         
     | 
| 7 | 
         
            +
            sdk_version: 5.12.0
         
     | 
| 8 | 
         
            +
            app_file: app.py
         
     | 
| 9 | 
         
            +
            pinned: false
         
     | 
| 10 | 
         
            +
            short_description: This is a demo for information extraction from Egyption IDs
         
     | 
| 11 | 
         
            +
            ---
         
     | 
| 12 | 
         
            +
             
     | 
| 13 | 
         
            +
            Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
         
     | 
    	
        app.py
    ADDED
    
    | 
         @@ -0,0 +1,102 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            import gradio as gr
         
     | 
| 2 | 
         
            +
            from recognization.recognization import TextRecognition
         
     | 
| 3 | 
         
            +
            from detection.recognize_id.detect_and_recognize_id import Recognize_ID
         
     | 
| 4 | 
         
            +
            from detection.detection import detection
         
     | 
| 5 | 
         
            +
            import os
         
     | 
| 6 | 
         
            +
            import numpy as np
         
     | 
| 7 | 
         
            +
            import cv2
         
     | 
| 8 | 
         
            +
             
     | 
| 9 | 
         
            +
            # Define a function to crop the image based on bounding box coordinates
         
     | 
| 10 | 
         
            +
            def crop_image(image, x, y, w, h):
         
     | 
| 11 | 
         
            +
                """
         
     | 
| 12 | 
         
            +
                Crop the image based on the provided bounding box coordinates.
         
     | 
| 13 | 
         
            +
                x, y are the top-left coordinates, w is width, and h is height.
         
     | 
| 14 | 
         
            +
                """
         
     | 
| 15 | 
         
            +
                # Convert to integer values
         
     | 
| 16 | 
         
            +
                x, y, w, h = int(x), int(y), int(w), int(h)
         
     | 
| 17 | 
         
            +
                
         
     | 
| 18 | 
         
            +
                # Convert the image to a numpy array if it's in PIL format (e.g., if it came from Gradio)
         
     | 
| 19 | 
         
            +
                if isinstance(image, np.ndarray):
         
     | 
| 20 | 
         
            +
                    img = image
         
     | 
| 21 | 
         
            +
                else:
         
     | 
| 22 | 
         
            +
                    img = np.array(image)
         
     | 
| 23 | 
         
            +
             
     | 
| 24 | 
         
            +
                # Crop the image using the coordinates
         
     | 
| 25 | 
         
            +
                cropped_img = img[y:y+h, x:x+w]
         
     | 
| 26 | 
         
            +
                return cropped_img
         
     | 
| 27 | 
         
            +
             
     | 
| 28 | 
         
            +
            # Define a dummy prediction function for license card data extraction
         
     | 
| 29 | 
         
            +
            def predict_image(image):
         
     | 
| 30 | 
         
            +
             
     | 
| 31 | 
         
            +
                # Recognize ID (adjust for license cards as needed)
         
     | 
| 32 | 
         
            +
                rec_id = Recognize_ID()
         
     | 
| 33 | 
         
            +
                id = rec_id.give_me_id_number(image)
         
     | 
| 34 | 
         
            +
             
     | 
| 35 | 
         
            +
                # Detection (update the detection method to work with license card layout)
         
     | 
| 36 | 
         
            +
                det = detection()
         
     | 
| 37 | 
         
            +
                detection_list = det.full_pipeline(image)
         
     | 
| 38 | 
         
            +
             
     | 
| 39 | 
         
            +
                result = ''
         
     | 
| 40 | 
         
            +
                # Loop through the detection list, extracting text from the new fields (update to use the license card coordinates)
         
     | 
| 41 | 
         
            +
                recognizer = TextRecognition()
         
     | 
| 42 | 
         
            +
             
     | 
| 43 | 
         
            +
                # Bounding box classes based on provided coordinates
         
     | 
| 44 | 
         
            +
                bounding_boxes = [
         
     | 
| 45 | 
         
            +
                    (0.693423, 0.231959, 0.249721, 0.055670),
         
     | 
| 46 | 
         
            +
                    (0.692308, 0.288660, 0.251951, 0.045361),
         
     | 
| 47 | 
         
            +
                    (0.731327, 0.350515, 0.158305, 0.057732),
         
     | 
| 48 | 
         
            +
                    (0.310479, 0.354639, 0.166109, 0.041237),
         
     | 
| 49 | 
         
            +
                    (0.608696, 0.426804, 0.405797, 0.070103),
         
     | 
| 50 | 
         
            +
                    (0.749721, 0.502062, 0.132664, 0.059794),
         
     | 
| 51 | 
         
            +
                    (0.737458, 0.558763, 0.139353, 0.053608),
         
     | 
| 52 | 
         
            +
                    (0.296544, 0.554639, 0.066890, 0.057732),
         
     | 
| 53 | 
         
            +
                    (0.672798, 0.808247, 0.143813, 0.049485)
         
     | 
| 54 | 
         
            +
                ]
         
     | 
| 55 | 
         
            +
             
     | 
| 56 | 
         
            +
                # Convert the image to numpy array if necessary (e.g., if it's a PIL image)
         
     | 
| 57 | 
         
            +
                if isinstance(image, np.ndarray):
         
     | 
| 58 | 
         
            +
                    img = image
         
     | 
| 59 | 
         
            +
                else:
         
     | 
| 60 | 
         
            +
                    img = np.array(image)
         
     | 
| 61 | 
         
            +
             
     | 
| 62 | 
         
            +
                # Extract and recognize text from the specific bounding boxes
         
     | 
| 63 | 
         
            +
                for bbox in bounding_boxes:
         
     | 
| 64 | 
         
            +
                    x, y, w, h = bbox
         
     | 
| 65 | 
         
            +
                    # Convert relative coordinates to absolute pixel values (assuming image dimensions are available)
         
     | 
| 66 | 
         
            +
                    h, w_img = img.shape[:2]
         
     | 
| 67 | 
         
            +
                    x_abs = int(x * w_img)
         
     | 
| 68 | 
         
            +
                    y_abs = int(y * h)
         
     | 
| 69 | 
         
            +
                    w_abs = int(w * w_img)
         
     | 
| 70 | 
         
            +
                    h_abs = int(h * h)
         
     | 
| 71 | 
         
            +
             
     | 
| 72 | 
         
            +
                    # Crop the image using OpenCV
         
     | 
| 73 | 
         
            +
                    cropped_image = crop_image(img, x_abs, y_abs, w_abs, h_abs)
         
     | 
| 74 | 
         
            +
                    
         
     | 
| 75 | 
         
            +
                    # Recognize text in the cropped image
         
     | 
| 76 | 
         
            +
                    recognized_word = recognizer.recognize_image(cropped_image)
         
     | 
| 77 | 
         
            +
                    result = result + recognized_word + ' '
         
     | 
| 78 | 
         
            +
                    result += '\n'
         
     | 
| 79 | 
         
            +
             
     | 
| 80 | 
         
            +
                # Add ID number
         
     | 
| 81 | 
         
            +
                result = result + id
         
     | 
| 82 | 
         
            +
             
     | 
| 83 | 
         
            +
                return result
         
     | 
| 84 | 
         
            +
             
     | 
| 85 | 
         
            +
            # List of paths to your sample images
         
     | 
| 86 | 
         
            +
            current_dir = os.path.dirname(os.path.abspath(__file__))
         
     | 
| 87 | 
         
            +
            sample_images = [
         
     | 
| 88 | 
         
            +
                os.path.join(current_dir, "samples/license_card_sample.png")  # Update to your sample license card image
         
     | 
| 89 | 
         
            +
            ]
         
     | 
| 90 | 
         
            +
             
     | 
| 91 | 
         
            +
            # Create the Gradio interface
         
     | 
| 92 | 
         
            +
            interface = gr.Interface(
         
     | 
| 93 | 
         
            +
                fn=predict_image,  # Function to run    
         
     | 
| 94 | 
         
            +
                inputs="image",    # Input type
         
     | 
| 95 | 
         
            +
                outputs="text",    # Output type
         
     | 
| 96 | 
         
            +
                title="Information extraction from License Cards",  # Updated title for license cards
         
     | 
| 97 | 
         
            +
                description="Upload a license card image to extract data.",
         
     | 
| 98 | 
         
            +
                examples=sample_images
         
     | 
| 99 | 
         
            +
            )
         
     | 
| 100 | 
         
            +
             
     | 
| 101 | 
         
            +
            # Launch the app
         
     | 
| 102 | 
         
            +
            interface.launch()
         
     | 
    	
        detection.pt
    ADDED
    
    | 
         @@ -0,0 +1,3 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            version https://git-lfs.github.com/spec/v1
         
     | 
| 2 | 
         
            +
            oid sha256:99ec8e866ec9193070894776a3a9183b1fc51fcc5b374abb26f72585bf31266a
         
     | 
| 3 | 
         
            +
            size 40513509
         
     | 
    	
        recognization_id.pt
    ADDED
    
    | 
         @@ -0,0 +1,3 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            version https://git-lfs.github.com/spec/v1
         
     | 
| 2 | 
         
            +
            oid sha256:929c9b568d2c4ece0a993d5f5d327ccac21a0edc1d91bdee904d27a590e87dcd
         
     | 
| 3 | 
         
            +
            size 57156628
         
     | 
    	
        recognization_model.pth
    ADDED
    
    | 
         @@ -0,0 +1,3 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            version https://git-lfs.github.com/spec/v1
         
     | 
| 2 | 
         
            +
            oid sha256:3a12da308d75a0b39d0f8b6691b8cd4a0956375d88e0e8fe0443ff9855f45737
         
     | 
| 3 | 
         
            +
            size 189602122
         
     | 
    	
        requirements.txt
    ADDED
    
    | 
         @@ -0,0 +1,6 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            torch
         
     | 
| 2 | 
         
            +
            numpy
         
     | 
| 3 | 
         
            +
            tqdm
         
     | 
| 4 | 
         
            +
            opencv-python
         
     | 
| 5 | 
         
            +
            ultralytics
         
     | 
| 6 | 
         
            +
             
     |