Spaces:
Sleeping
Sleeping
File size: 2,078 Bytes
dac704b |
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 |
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()
|