yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
RPi AI Camera📖
01.Preparation before use
  • Preparation before use
02.Environment construction
  • Environment construction
03.rpicam-apps examples
  • rpicam-apps examples
04.PiCamera2 Examples
  • PiCamera2 Examples
05.Mediapipe Course
  • 1. Face effects
  • 2. Face Detection
  • 3. 3D Object Recognition
  • 4. Brush
  • 5. Finger control
  • 6. Gesture Recognition
06.Advanced AI Course
  • 1. Human Pose Estimation
  • 2. Object Detection
  • 3. yolov5
  • 4. QR code
  • 5. Face Detection
  • 6. Color recognition
Download
  • System_File
  • Code
  • Software Tools
Download ZIP

Welcome to RPi AI Camera repository

2. Object Detection

Object Detection

2. Object Detection

2. Object Detection2. Object Detection2.1, Model structure2.2, Code Analysis2.3, Startup

2. Object Detection

This section mainly addresses the problem of how to use the dnn module in OpenCV to import a trained object detection network. However, there are requirements for the version of opencv.

Currently, there are three main methods for object detection using deep learning:

  • Faster R-CNNs

  • You Only Look Once (YOLO)

  • Single Shot Detectors (SSDs)

Faster R-CNNs is the most commonly heard neural network based on deep learning. However, this method is technically difficult to understand (especially for deep learning novices), difficult to implement, and difficult to train.

In addition, even if the "Faster" method is used to implement R-CNNs (here R stands for Region Proposal), the algorithm is still relatively slow, about 7FPS.

If we pursue speed, we can turn to YOLO, because it is very fast, reaching 40-90 FPS on TianXGPU, and the fastest version may reach 155 FPS. But the problem with YOLO is that its accuracy needs to be improved.

SSDs was originally developed by Google and can be said to be a balance between the above two. Compared with Faster R-CNNs, its algorithm is more direct. Compared with YOLO, it is more accurate.

2.1, Model structure

The main work of MobileNet is to replace the past standard convolutions with depthwise sparable convolutions to solve the problems of computational efficiency and parameter quantity of convolutional networks. The MobileNets model is based on depthwise sparable convolutions, which can decompose standard convolutions into a depth convolution and a point convolution (1 × 1 convolution kernel). Deep convolution applies each convolution kernel to each channel, while 1 × 1 convolution is used to combine the output of channel convolution.

Batch Normalization (BN) is added to the basic components of MobileNet, that is, at each SGD (stochastic gradient descent), the standardization process is performed so that the mean of the result (each dimension of the output signal) is 0 and the variance is 1. Generally, when you encounter slow convergence or gradient explosion during neural network training, you can try to solve the problem. In addition, in general use, you can also add BN to speed up training and improve model accuracy.

In addition, the model also uses the ReLU activation function, so the basic structure of the depthwise separable convolution is shown in the figure below:

Network Analysis (II): MobileNets Detailed Explanation

The MobileNets network is composed of many depthwise separable convolutions shown in the figure above. The specific network structure is shown in the figure below:

Network Analysis (II): Detailed Explanation of MobileNets

2.2, Code Analysis

List of Recognizable Objects

xxxxxxxxxx
[person, bicycle, car, motorcycle, airplane, bus, train,
truck, boat, traffic light, fire hydrant, street sign,
stop sign, parking meter, bench, bird, cat, dog, horse,
sheep, cow, elephant, bear, zebra, giraffe, hat, backpack,
umbrella, shoe, eye glasses, handbag, tie, suitcase,
frisbee, skis, snowboard, sports ball, kite, baseball bat,
baseball glove, skateboard, surfboard, tennis racket,
bottle, plate, wine glass, cup, fork, knife, spoon, bowl,
banana, apple, sandwich, orange, broccoli, carrot, hot dog,
pizza, donut, cake, chair, couch, potted plant, bed, mirror,
dining table, window, desk, toilet, door, tv, laptop, mouse,
remote, keyboard, cell phone, microwave, oven, toaster,
sink, refrigerator, blender, book, clock, vase, scissors,
teddy bear, hair drier, toothbrush]

Load category [object_detection_coco.txt], import model [frozen_inference_graph.pb], specify deep learning framework [TensorFlow]

xxxxxxxxxx
# Load COCO class names
with open('object_detection_coco.txt', 'r') as f: class_names = f.read().split('\n')
# Display different colors for different targets
COLORS = np.random.uniform(0, 255, size=(len(class_names), 3))
# Load DNN image model
model = cv.dnn.readNet(model='frozen_inference_graph.pb', config='ssd_mobilenet_v2_coco.txt', framework='TensorFlow')

Import the image, extract the height and width, calculate the 300x300 pixel blob, and pass this blob into the neural network

xxxxxxxxxx
def Target_Detection(image):
image_height, image_width, _ = image.shape
# Create a blob from the image
blob = cv.dnn.blobFromImage(image=image, size=(300, 300), mean=(104, 117, 123), swapRB=True)
model.setInput(blob)
output = model.forward()
# Iterate over each detection
for detection in output[0, 0, :, :]:
# Extract the confidence of the detection
confidence = detection[2]
# Draw bounding box only if detection confidence is above a certain threshold, skip otherwise
if confidence > .4:
# Get class ID
class_id = detection[1]
# Map class id to class
class_name = class_names[int(class_id) - 1]
color = COLORS[int(class_id)]
# Get bounding box coordinates
box_x = detection[3] * image_width
box_y = detection[4] * image_height
# Get bounding box width and height
box_width = detection[5] * image_width
box_height = detection[6] * image_height
# Draw a rectangle around each detected object
cv.rectangle(image, (int(box_x), int(box_y)), (int(box_width), int(box_height)), color, thickness=2)
# Write class name text on detected objects
cv.putText(image, class_name, (int(box_x), int(box_y - 5)), cv.FONT_HERSHEY_SIMPLEX, 1, color, 2)
return image

2.3, Startup

Note: The AI camera in this case does not have a computing power bonus, and is called as a normal camera!