[Python] Developing object detection system using flet and opencv

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

  1. Stack

    Stack Membro Participativo

    My interest is related to the artificial intelligence and specifically i want to develop a flat app, that using camera, detects object(person in this case) and will calculate its age and gender, in the future i am going to use this app in mobile development side(ios or android systems), therefore i decided to create a simple app for this task

    here is my code :

    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+10
    page.theme_mode ='light'
    page.add(section)
    # page.update()
    if __name__=="__main__":
    ft.app(target=main)
    cap.release()
    cv2.destroyAllWindows()


    result of this code is given as :

    flet result

    camera turns on but i can't see image, i think that there is some kind of overlapping maybe between text and camera, can you tell me please what is source of problem?

    Continue reading...

Compartilhe esta Página