PaddleHub-Lightweight code implementation to call pre-trained model to achieve target detection

Because I need to do a single-chip microcomputer course design, the topic is smart fan, I tried many sensors, and I felt that the fan is not smart enough, so I want to directly use an AI model for face detection or pedestrian detection to detect the position of the person, and then realize the fan blows on the person. If a person is absent, it will automatically turn off, realizing the intelligentization of the fan.
Because the AI ​​model is not the focus of the project, I don't want to waste too much time on training the model, so I chose the pre-training model in the paddlehub model library to complete the function.

Install PaddleHub

pip3 install paddlehub

I also encountered a situation where the installation of opencv-python has been stuck, and the installation did not work all night, so I directly installed opencv-python manually:

Stuck here!
Insert picture description here
Manual installation steps:

  • opencv_python-4.4.0.46-cp37-cp37m-linux_armv7l.whlDownload the package on this page :
https://www.piwheels.org/simple/opencv-python/opencv_python-4.4.0.46-cp37-cp37m-linux_armv7l.whl
  • Manual installation:
pip3 install opencv_python-4.4.0.46-cp37-cp37m-linux_armv7l.whl

10s installation is complete.
Insert picture description hereThen reinstall paddlehub.

Go to the official website to choose the right model

PaddleHub model library

In this course design, considering that the Raspberry Pi has limited computing power and does not require particularly high recognition accuracy, I chose a pyramidbox_lite_mobilemodel to achieve face detection:
Insert picture description here

Prediction code

import paddlehub as hub
import cv2
import time

def draw_bbox_image(frame, boxes):
    cv2.rectangle(frame, (boxes["left"], boxes["top"]), (boxes["right"], boxes["bottom"]), (0,255,0), 2)
    #cv2.putText(图像, 文字, (x, y), 字体, 大小, (b, g, r), 宽度)
    cv2.putText(frame, str(boxes["confidence"]), (boxes["left"], boxes["top"]-2), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)


if __name__ == "__main__":
    vedio_capter = cv2.VideoCapture(0)
    face_detector = hub.Module(name="pyramidbox_lite_mobile")
    
    while True:
        ret, frame = vedio_capter.read() 
        tim1 = time.time()
        result = face_detector.face_detection(images=[frame])
        try:
            data_dict = result[0]["data"][0]
            draw_bbox_image(frame, data_dict)
            print("data_dict:", data_dict)
        except IndexError:
            pass
        cv2.imshow("result", frame)
        tim2 = time.time()
        tim = tim2 - tim1
        print("tim", tim)
        print("result", result)
        if cv2.waitKey(1) & 0xFF == ord('q'):
                break

    vedio_capter.realse() 

Windows end realization effect

Insert picture description here

Finished!

Go to make a microcontroller, hehehe.

Suddenly learned that PaddlePaddle does not support arm architecture, so paddlehub cannot be used on Raspberry Pi.

Then, by the way, hang up an article about the communication between PYB MCU and Raspberry Pi. Interested friends can directly make it out, and it will be done in 1 hour!

Guess you like

Origin blog.csdn.net/qq_45779334/article/details/110311009