Welcome to Rider Pi Robot repository
19.Motion Detection
Motion Detection
Motion Detection1. Experiment Objective2. Experiment Steps3. Experimental Phenomena4. Experimental source code analysis
This tutorial is specific to CM5; it cannot be used directly in CM4.
1. Experiment Objective
To learn how to use the Rider-Pi's camera and screen to create a motion statistics health assistant. The counting here mainly involves tracking the detected human motion movements.
2. Experiment Steps
Enter the Rider-Pi system, terminate the main Rider-Pi program, and enter "ip (where IP is the Rider-Pi's IP):8888" in your browser. After logging in, enter the password "yahboom".

Then log in and navigate to the path cd /home/pi/Rider-pi_class/5.AI Visual Recognition Course/19. Motion detection and run mian.ipynb.
Finally, run the program.
3. Experimental Phenomena
- After the program runs, select the movement to be detected using the button in the upper right corner of the screen. The detectable movements are as follows:
- Squat
- Push-up
- Sit-up
- Bicep curl
- Lateral raise
- Overhead press
- Leg raise
- Knee raise
- Left knee press
- Right knee press


- After selecting the corresponding mode/function, press the button in the upper left corner of the screen to start the movement statistics.


- To change the detected motion, restart the program and select the mode again.
4. Experimental source code analysis
xxxxxxxxxxwhile True: frame = picam2.capture_array() processed_frame, current_angle, keypoints = pose_processor.process_frame(frame, exercise_type) # 将OpenCV图像转为PIL图像 # Convert OpenCV images to PIL images pil_img = Image.fromarray(cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(pil_img) current_count = exercise_counter.counter # 计算文本位置 Calculate text position text_width = draw.textlength(display_text, font=pil_font) count_width = draw.textlength(str(current_count), font=pil_font) right_margin = 5 text_x = processed_frame.shape[1] - text_width - right_margin count_x = processed_frame.shape[1] - count_width - right_margin # 绘制文本 Drawing Text draw.text((text_x, 30), display_text, font=pil_font, fill=(0, 255, 0)) draw.text((count_x, 60), str(current_count), font=pil_font, fill=(0, 255, 0)) # 转换回OpenCV格式 Convert back to OpenCV format processed_frame = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR) image_widget.value = bgr8_to_jpeg(myimg) if button.press_b(): breakThis while loop is the main business logic for motion detection. It exits the detection by detecting the button in the lower left corner of the screen.