[Python] Developing live object detection system usng flet

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    i like to develope flet app, that runs camera(built in camera) and detects gender and age of person, result should be something like this : gender detection

    for this purpose, i have developed following code in flet

    from flet import *
    import base64
    import dlib
    import cv2
    import math
    import flet as ft
    from flet_core import UserControl

    cap =cv2.VideoCapture(0)
    detector =dlib.get_frontal_face_detector()
    predictor =dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    def estimate_age(shape):
    eye_distance =math.sqrt((shape.part(45).x-shape.part(36).x)**2+(shape.part(45).y-shape.part(36).y)**2)
    age =eye_distance*0.3
    return int(age)

    def estimate_gender(shape):
    lip_distance =math.sqrt((shape.part(62).x-shape.part(66).x)**2+(shape.part(62).y-shape.part(66).y)**2)
    if lip_distance>25:
    gender ="Male"
    else:
    gender ="Female"
    return gender

    class YourFace(UserControl):
    def __init__(self):
    super().__init__()
    self.your_gender =Text(
    value ="",size=30,weight=ft.FontWeight(value="bold"),
    color ='white'
    )
    self.your_age = Text(
    value="", size=30, weight=ft.FontWeight(value="bold"),
    color='white'
    )
    def did_mount(self):
    self.update_timer()
    def update_timer(self):
    while True:
    _,frame =cap.read()
    gray =cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    rects =detector(gray,0)
    if len(rects)>0:
    for rect in rects:
    x,y,w,h =rect.left(),rect.top(),rect.width(),rect.height()
    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
    shape =predictor(gray,rect)
    age =estimate_age(shape)
    gender =estimate_gender(shape)
    cv2.putText(frame,f'your age is {age}',(x,y-10),
    cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)
    cv2.putText(frame, f'your gender is {gender}', (x, y+h + 10),
    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    self.your_age.value =age
    self.your_gender.value =gender
    self.page.update()
    else:
    cv2.putText(frame, "no person ", (10,30),
    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    _,im_arr =cv2.imencode(".png",frame)
    im_b64 =base64.b64encode(im_arr)
    self.img.src_base64 =im_b64.decode('utf-8')
    self.update()
    def build(self):
    self.img =Image(
    border_radius=border_radius.all(20)

    )
    return Column(
    [
    self.img,
    Row([
    Text("Gender :",size=30,weight="bold",color='white'),
    self.your_gender
    ]),

    Row([
    Text("Age :", size=30, weight="bold", color='white'),
    self.your_age
    ]),
    ]
    )


    section =Container(

    margin=margin.only(bottom=40),
    content=Row(
    [
    Card(
    elevation=30,
    content=Container(
    bgcolor='blue',
    padding=10,
    border_radius=border_radius.all(20),
    content=Column([
    Text("Face Cam Flet",size=30,
    weight="bold",color='white'),
    YourFace()
    ])

    )
    )
    ],alignment='center'

    )
    )


    def main(page:page):
    page.padding =30
    page.window.left =page.window.left+100
    page.theme_mode ='light'
    page.add(section)
    # page.update()
    # page.update()
    if __name__=="__main__":
    ft.app(target=main)
    cap.release()
    cv2.destroyAllWindows()


    this code does not show any errors, but it simple returns window without live camera, here is result : result of code

    my interest is : in which part i am writing wrong codes?please give me some hints that i can easily solve t

    Continue reading...

Compartilhe esta Página