Spaces:
Sleeping
Sleeping
import easyocr | |
import json | |
import gradio as gr | |
# Initialize reader for your language (assuming English and French are relevant) | |
reader = easyocr.Reader(['en', 'fr']) | |
def extract_inbody_data(image): | |
# Extract text from the image | |
result = reader.readtext(image, detail = 0) | |
# Process the extracted text into structured format | |
try: | |
#print(result) | |
processed_output = extract_and_convert(result) | |
# Convert to JSON | |
json_output = json.dumps(processed_output, indent = 2) | |
return json_output | |
except (IndexError, ValueError) as e: | |
return f"Error processing image: {str(e)}" | |
# Function to extract numbers and convert to float | |
def extract_and_convert(data): | |
# Extract elements that represent valid numbers (using isdigit or casting as float) | |
numbers = [] | |
for item in data: | |
try: | |
# Convert to float if possible | |
num = float(item) | |
numbers.append(num) | |
except ValueError: | |
continue # Ignore non-numeric values | |
# Ensure we have exactly 6 values (height, age, weight, MMS, body fats, and ratio) | |
if len(numbers) >= 6: | |
height = numbers[0] | |
age = numbers[1] | |
weight = correct_float(numbers[2]) | |
mms = correct_float(numbers[3]) | |
body_fats = correct_float(numbers[4]) | |
ratio = numbers[5] | |
return { | |
"Height": height, | |
"Age": age, | |
"Weight": weight, | |
"MMS": mms, | |
"Body Fats": body_fats, | |
"Ratio": ratio | |
} | |
else: | |
return {} | |
def correct_float(num): | |
if num > 100: | |
return round(num/10,2) | |
else: | |
return num | |
# Create Gradio Interface | |
interface = gr.Interface( | |
fn = extract_inbody_data, | |
inputs = gr.Image(type = "filepath", label = "Upload InBody Image"), | |
outputs = "json", | |
title = "InBody Data Extractor", | |
description = "Upload an InBody machine screen image and extract health data (Taille, Age, Poids, MMS, TGC).", | |
) | |
if __name__ == '__main__': | |
interface.launch() | |