yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Raspbot V2📖
1.About Raspbot V2
  • 1. First trial
  • 2. Expansion board interface distribution
  • 3. About battery and charging
  • 4. Install servo PTZ
  • 5.Servo Gimbal Installation
  • 6. Important Notes (Read Before Starting)
2.Assembly
  • Installation Video
3.Car basic course
  • 0. Preparation before development
  • 1.Buzzer control
  • 2.Key reading
  • 3.RGB control
  • 4.Servo control (PTZ control)
  • 5.Motor control
  • 6.Printing ultrasonic data
  • 7.Tracking module status printing
  • 8.IR control key value printing
4.Car motion control course
  • 0.Mecanum wheel motion principle
  • 1.Car vertical movement
  • 2.Car diagonal movement
  • 3.Car sideways movement
  • 4.Car rotation movement
  • 5.Car patrol
5.Car advanced courses
  • 1.Infrared patrol car
  • 2.Ultrasonic avoid
  • 3.Ultrasonic follow
  • 4.Infrared tracking+ ultrasonic avoid
  • 5.Restricting
  • 6.Infrared remote control car
6.OpenCV courses
A. Getting Started with OpenCV
  • 1. Introduction to OpenCV
  • 2.OpenCV image reading and display
  • 3. OpenCV image writing
  • 4. OpenCV Image Quality
  • 5.OpenCV pixel operations
B. OpenCV geometric transformation
  • 1. Image scaling
  • 2. Image cutting
  • 3. Image translation
  • 4. Image mirroring
  • 5. Affine Transformation
  • 6. Image Rotation
  • 7. Perspective Transformation
C. OpenCV image processing and text line drawing
  • 1. Grayscale processing
  • 2. Image Binarization
  • 3. Edge Detection
  • 4. Line drawing
  • 5. Rectangle and circle drawing
  • 6. Text and image drawing
D. Image beautification
  • 1. Color Image Histogram
  • 2. Histogram Equalization
  • 3. Image repair
  • 4. Brightness enhancement
  • 5. Smooth and whiten
  • 6. Gaussian mean filtering
  • 7. Median Filter
E. Machine Learning
  • 1. KNN
  • 2. SVM
7.AI vision recognition course
  • 1.Camera driver tutorial
  • 2.Color recognition
  • 3.HSV value test
  • 4.Object Recognition
  • 5.QR code recognition
  • 6.Face recognition
  • 7.License Plate Recognition
  • 8. Human Pose Estimation
  • 9. Target detection
  • 10.yolov5-lite
  • 11. Gesture Recognition
  • 12. Face Detection
  • 13. Face effects
  • 14. 3D Object Recognition
  • 15. Paintbrush
  • 16. Finger control
8.AI vision interaction module
  • 1. Color tracking
  • 2. Color follows
  • 3. QR code recognition to control movement
  • 4. Face tracking
  • 5. Face following
  • 6. Visual tracking
  • 7. Garbage identification
  • 8. Track map Autopilot(purchased separately)
  • 9. Gesture following
9.Voice control basic course(For Superior Kit)new
  • 1.Voice control car movement
  • 2.Voice control RGB light
  • 3.Voice control color recognition
  • 4.Voice control autonomous driving
  • 5.Voice control car tracking
10.AI large model knowledge(For Superior Kit)new
  • 1.AI large model types and principles
  • 2.RAG retrieval enhancement and model training samples
  • 3.Embodied intelligent robot system architecture
  • 4.Embodied intelligent functions core source code
  • 5.Configure AI large model(Required)
11.AI Large Model Development(For Superior Kit & Reference)new
  • 1.Register a model service provider account
  • 2. Configuring API-KEY(Optional)
  • 3. Introduction to Dify
  • 4. Basic Dify Features
  • 5. Deploy the RAG knowledge base
  • 6. AI Large Model Development - chatbot
  • 7. RAG knowledge base + chatbot
  • 8. AI agent workflow
  • 9. Custom wake-up response
  • 10. Core module testing tools
12.AI large basic course(For Superior Kit)new
  • 1.Free conversation
  • 2.Text-to-picture
  • 3.Scene description
  • 4.Video description
  • 5.Intelligent action choreography
  • 6.Dual-model agent application
13.ROS2-Robot chassis control course
  • 1. Robot information release
  • 2. Robot keyboard control
  • 3. URDF Model
14.ROS2-OpenCV course
  • 1.ROS+Opencv application
  • 2. AR Vision
  • 3. Mediapipe fun gameplay
  • 4. Gesture control car movement
  • 5. Gesture control car actions
  • 6. Palm control car movement
  • 7. Palm control servo PTZ
  • 8. Visual tracking
  • 9. AprilTag code recognition
  • 10. AprilTag tag code tracking
  • 11. AprilTag tag code follow
15.Development environment course
  • 1. Introduction to Raspberry Pi 5
  • 2. Image writing and backup
  • 3. Start the Raspberry Pi 5
  • 4. Remote Access
  • 5. Remote file transfer
  • 6. Common Linux commands and use of vim editor
  • 7. Install Raspbot V2 driver library
  • 8. Jupyter Lab environment construction
  • 9. Jupyter Lab plugin installation
  • 10. Basic use of Jupyter Widgets
16.Raspberry Pi basic settings
  • 1.Update and upgrade the operating system
  • 2. Introduction to raspi-config tool
  • 3. config.txt file description
  • 4. Network Configuration
  • 5. Set display resolution and rotation
  • 6. Set screen sleep
  • 7. Get real-time temperature of RPi
  • 8. Play audio and video
  • 9. Use USB Camera
  • 10.Use MIPI camera
17.Docker course
  • 1. Overview and installation
  • 2. Common commands
  • 3. Understand and publish images
  • 4. Docker data processing
  • 5. Enter docker container
18.ROS2 basic course
  • 1. Introduction to ROS2
  • 2.ROS2 install Humble
  • 3. ROS2 development environment
  • 4. ROS2 workspace
  • 5. ROS2 function package
  • 6. ROS2 node
  • 7.ROS2 topic communication
  • 8. ROS2 service communication
  • 9. ROS2 action communication
  • 10. ROS2 custom interface message
  • 11. ROS2 parameter service case
  • 12. ROS2 meta-function package
  • 13. ROS2 distributed communication
  • 14. ROS2 DDS
  • 15. ROS2 time related API
  • 16. ROS2 common command tools
  • 17. ROS2 rviz2 use
  • 18. ROS2 rqt toolbox
  • 19. ROS2 Launch startup file configuration
  • 20. ROS2 recording and playback tool
  • 21. ROS2 URDF model
  • 22. ROS2 Gazebo simulation platform
  • 23.ROS2 TF2 coordinate transformation
Download
  • APP(iOS/Android)
  • Code
  • System_File
  • Instruction Manual
  • Hardware_Info
  • 3D_Model_File
  • ROS2_Basic_VM
Download ZIP

Welcome to Raspbot V2 repository

当製品の日本語版マニュアル資料は、こちらからご取得ください。

4.Object Recognition

4. Object Recognition

4. Object Recognition4.1. Introduction to TensorFlow4.2. Object Recognition

4.1. Introduction to TensorFlow

TensorFlow is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while graph edges represent multidimensional data arrays (tensors) that flow between them. This flexible architecture allows you to deploy computations to one or more CPUs or GPUs in a desktop, server, or mobile device without rewriting your code. The TensorFlow User Guide provides a detailed overview and describes how to use and customize the TensorFlow deep learning framework. TensorFlow was originally developed by researchers and engineers in the Google Brain team in Google's Machine Intelligence Research organization for machine learning and deep neural network (DNN) research. The system is general enough to be applicable to a variety of other fields.

What is a Data Flow Graph?

A data flow graph describes mathematical computations as a directed graph of "nodes" and "edges". "Nodes" are generally used to represent mathematical operations applied, but can also represent the starting point of data input (feed in)/the end point of output (push out), or the end point of reading/writing persistent variables (persistent variables). "Wires" represent input/output relationships between "nodes". These data "wires" can transport multi-dimensional data arrays of "dynamically adjustable size", namely "tensors". The intuitive image of tensors flowing through the graph is the reason why this tool is named "Tensorflow". Once all tensors on the input end are ready, the nodes will be assigned to various computing devices to complete asynchronous and parallel execution of operations.

Key features of TensorFlow:

  1. Data flow graph: The core concept of TensorFlow is the data flow graph, in which nodes represent mathematical operations and edges represent multi-dimensional data arrays (tensors) flowing between nodes. This graphical representation makes the calculation process clear and easy to optimize and parallelize.
  2. Tensors and operations: In TensorFlow, data is represented as tensors, and operations (such as addition, multiplication, matrix operations, etc.) are defined as nodes in the graph. This structure enables the framework to handle a variety of tasks from simple mathematical operations to complex deep learning models.
  3. Automatic differentiation: TensorFlow can automatically calculate the gradient of complex computational graphs, which is crucial for training neural networks. This means that users do not have to implement gradient calculations manually, saving a lot of time and effort.
  4. Flexibility and modularity: TensorFlow provides a large number of modules and APIs that allow users to build complex models, while also enhancing their functionality through plug-ins and extensions. In addition, the TensorFlow 2.x version introduces the Eager Execution mode, which makes the code more intuitive and closer to immediate execution, making it easier to debug and prototype.
  5. High-performance computing: TensorFlow is able to leverage hardware accelerators such as GPUs and TPUs (tensor processing units) to achieve efficient training and reasoning on large-scale data sets.
  6. Distributed computing: TensorFlow supports distributed computing, allowing models to be trained in parallel on multiple devices or servers, which is especially important for processing large-scale data sets.
  7. High-level API: In addition to the underlying API, TensorFlow also provides high-level APIs such as Keras, making model building and experimentation easier while maintaining access to the underlying details.
  8. Model service and deployment: TensorFlow provides tools for model deployment and management, such as TensorFlow Serving, so that models can be easily used in production environments.
  9. Community and Ecosystem: TensorFlow has a large developer community and a rich ecosystem, including pre-trained models, data sets, and various application examples, which greatly promotes learning and innovation.
  10. Education and Documentation: TensorFlow provides detailed documentation and tutorials, as well as related educational materials, to help novices and professionals learn and master machine learning techniques.
  11. Multi-language support: In addition to the Python interface, TensorFlow also supports languages such as C++, Java, Go, and R, allowing it to be integrated into different development environments.

4.2. Object Recognition

Source code path:

/home/pi/project_demo/07.AI_Visual_Recognition/04.Tensorflow_object_recognition/04_object_recognition.ipynb

#导入oled屏幕库 Import oled screen library
import sys
sys.path.append('/home/pi/software/oled_yahboom/')
from yahboom_oled import *
# 创建oled对象 Create an oled object
oled = Yahboom_OLED(debug=False)
import numpy as np
import cv2
import os,time
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_utils
import ipywidgets.widgets as widgets
from image_fun import bgr8_to_jpeg
​x
# Init camera 
cap = cv2.VideoCapture(0)  # 定义摄像头对象,参数0表示第一个摄像头 Define the camera object, parameter 0 represents the first camera
cap.set(3, 320) # set Width
cap.set(4, 240) # set Height
cap.set(5, 30)  #设置帧率 Setting the frame rate
# cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
# cap.set(cv2.CAP_PROP_BRIGHTNESS, 60) #设置亮度 -64 - 64  0.0 Set Brightness -64 - 64 0.0
# cap.set(cv2.CAP_PROP_CONTRAST, 50) #设置对比度 -64 - 64  2.0 Set Contrast -64 - 64 2.0
# cap.set(cv2.CAP_PROP_EXPOSURE, 156)  #设置曝光值 1.0 - 5000  156.0 Set the exposure value 1.0 - 5000 156.0

# from picamera2 import Picamera2, Preview
# import libcamera
# picam2 = Picamera2()  
# camera_config = picam2.create_preview_configuration(main={"format":'RGB888',"size":(320,240)})
# camera_config["transform"] = libcamera.Transform(hflip=1, vflip=1)
# picam2.configure(camera_config) 
# picam2.start() 


xxxxxxxxxx
image_widget = widgets.Image(format='jpg', width=640, height=480)
xxxxxxxxxx
# Init tf model

MODEL_NAME = 'ssdlite_mobilenet_v2_coco_2018_05_09' #fast
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb' 
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt') 
NUM_CLASSES = 90 
IMAGE_SIZE = (12, 8) 
fileAlreadyExists = os.path.isfile(PATH_TO_CKPT) 

if not fileAlreadyExists:
    print('Model does not exsist !')
    exit
xxxxxxxxxx
# LOAD GRAPH
print('Loading...')
detection_graph = tf.Graph() 
with detection_graph.as_default(): 
    od_graph_def = tf.compat.v1.GraphDef()
    with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: 
        serialized_graph = fid.read() 
        od_graph_def.ParseFromString(serialized_graph) 
        tf.import_graph_def(od_graph_def, name='')
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True) 
category_index = label_map_util.create_category_index(categories)
print('Finish Load Graph..')
xxxxxxxxxx
print(type(category_index))
xxxxxxxxxx
print("dict['Name']: ", category_index[1]['name'])
xxxxxxxxxx
# Main
oled.init_oled_process() #初始化oled进程 Initialize oled process
oled.add_line("OBJTYPE:", 1)
oled.add_line("None", 3)
oled.refresh()
t_start = time.time()
fps = 0
display(image_widget)
with detection_graph.as_default():
    with tf.compat.v1.Session(graph=detection_graph) as sess:
        while True:
            #frame = picam2.capture_array()
            ret, frame = cap.read()
#            frame = cv2.flip(frame, -1) # Flip camera vertically
#             frame = cv2.resize(frame,(320,240))
            ##############
            image_np_expanded = np.expand_dims(frame, axis=0) 
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') 
            detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') 
            detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') 
            detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') 
            num_detections = detection_graph.get_tensor_by_name('num_detections:0')

#             print('Running detection..') 
            (boxes, scores, classes, num) = sess.run( 
                [detection_boxes, detection_scores, detection_classes, num_detections], 
                feed_dict={image_tensor: image_np_expanded}) 

#             print('Done.  Visualizing..')
            # 应用非极大值抑制
            selected_indices = tf.image.non_max_suppression(
                np.squeeze(boxes),
                np.squeeze(scores),
                max_output_size=100,  # 根据需要设置最大输出数量
                iou_threshold=0.7)  # 调整重叠阈值
                
            # 使用 tf.gather 来高效地应用索引
            filtered_boxes = tf.gather(tf.squeeze(boxes), selected_indices)
            filtered_scores = tf.gather(tf.squeeze(scores), selected_indices)
            filtered_classes = tf.gather(tf.cast(tf.squeeze(classes), tf.int32), selected_indices)
            
            # 在会话中运行过滤操作
            (filtered_boxes, filtered_scores, filtered_classes) = sess.run([
                filtered_boxes,
                filtered_scores,
                filtered_classes
            ])
            
            # 可视化
            vis_utils.visualize_boxes_and_labels_on_image_array(
                frame,
                filtered_boxes,
                filtered_classes,
                filtered_scores,
                category_index,
                use_normalized_coordinates=True,
                line_thickness=8)
            
            for i in range(0, 10):
                if scores[0][i] >= 0.7:
                    print(category_index[int(classes[0][i])]['name'])
                    oled.clear()
                    objtype_str=category_index[int(classes[0][i])]['name']
                    oled.add_line("OBJTYPE:", 1)
                    oled.add_line(objtype_str, 3)
                    oled.refresh()
            ##############
            fps = fps + 1
            mfps = fps / (time.time() - t_start)
            cv2.putText(frame, "FPS:" + str(int(mfps)), (10,25), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0,255,0), 2)
            image_widget.value = bgr8_to_jpeg(frame)

            k = cv2.waitKey(1) & 0xff
            if k == 27:# press 'ESC' to quit
                cap.release()
                # 恢复屏幕基础数据显示 Restore basic data display on screen
                os.system("python3 /home/pi/software/oled_yahboom/yahboom_oled.py &")
                break

    
xxxxxxxxxx
cap.release()
# 恢复屏幕基础数据显示 Restore basic data display on screen
os.system("python3 /home/pi/software/oled_yahboom/yahboom_oled.py &")
# picam2.stop()
# picam2.close()
#最后需要释放掉摄像头的占用 Finally, you need to release the camera's occupancy

这个程序在JupyterLab运行帧率有点低的,有误识别的情况。运行时,我们可以看到oled会显示识别到的物体英文名字。在同个文件夹下的04_object_recognition_nms.ipynb文件,这个误识别率会相对低一点,帧率也会低一点。

在需要关闭的时候选中运行的这段代码点击停止按钮关闭程序,最后记得释放掉视频流才可以在其他地方使用摄像头。

HSV value test
QR code recognition

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文