In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to use TensorFlow to train and identify objects in video images, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Note: windows user name cannot appear in Chinese!
Install Python
Note: TensorFlow of Windows platform only supports 3.5.X version of Python to enter the Python3.5.2 download page. Select the Python installation package for Windows platform in Files, download and install it.
Install TensorFlow
Go to the TensorFlow on Windows download page, and this tutorial uses the easiest combination of CPU support only + Native pip.
Open cmd and enter the following instructions to download and install TensorFlow. The download location is python\ Lib\ site-packages\ tensorflow: open IDLE, and enter the following instructions: if the following results occur, the installation is successful:
If there is a problem, please refer to the frequently asked questions at the bottom of the TensorFlow on Windows download page.
Install Protoc
Protoc is used to compile the relevant program running files, go to the Protoc download page, and download a compressed package similar to the one shown below with win32. After unpacking, copy the protoc.exe in the bin folder to the c:\ windows\ system32 directory (used to configure the directory where protoc.exe is located into the environment variable).
Install git
Go to the git official website to download the git of the Windows platform. For more information on installation and configuration, please refer to this article.
Install the remaining components
Enter the following instructions in cmd to download and install relevant API running support components: note: Native pip will be affected by other Python applications on your computer. Bloggers install Anaconda before doing simulation, resulting in downloading jupyter and other related components to the site-packages folder in Anaconda, and the later call fails.
Download the code and compile
Enter the following code in cmd: download Google tensorflow/models code from github, which is usually downloaded to disk C by default.
Also go to the models folder in cmd and compile the code for Object Detection API:
Run notebook demo
Continue to run the following command under the models folder: the browser automatically opens and displays the following interface: enter the object_detection_tutorial.ipynb in the object_detection folder: click Run All in the Cell and wait for about three minutes (the blogger's computer is close to being scrapped), you can display the following result: modify the file path, you can detect your own picture: note: set the picture name to match the code description For example, image1.jpg TensorFlow Object Detection API provides five recognition models that can be called directly, and the default is the simplest ssd + mobilenet model.
You can directly modify MODEL_NAME to call other models as follows:
MODEL_NAME = 'ssd_inception_v2_coco_11_06_2017'MODEL_NAME =' rfcn_resnet101_coco_11_06_2017'MODEL_NAME = 'faster_rcnn_resnet101_coco_11_06_2017'MODEL_NAME =' faster_rcnn_inception_resnet_v2_atrous_coco_11_06_2017'
Replace the model with faster_rcnn_inception_resnet, and the results are as follows: the accuracy has been greatly improved, but the speed has slowed down, and it takes five minutes to get the results on the blogger's old computer.
Video object recognition
Google has published the complete code of this project on github. Next, we will add the corresponding module to the existing code to realize the recognition of objects in the video.
Step 1: download the cv2 package of opencv and download the opencv related library on the official website of Python. Click here to enter directly.
The version installed by the blogger is as follows: after the download is complete, execute the installation command in cmd
Pip install opencv_python-3.2.0.8-cp35-cp35m-win_amd64.whl
After the installation is complete, enter the command in IDLE
Import cv2
If no error is reported, the opencv-python library is imported successfully and the environment is matched successfully.
Step 2: introduce cv2 package into the original code step 3: add video identification code the main steps are as follows: 1. Use the VideoFileClip function to grab pictures from the video. two。 Use the fl_image function to replace the original picture with the modified picture, which is used to transmit each captured picture of object recognition. 3. All modified clip images are combined into a new video.
Based on the original code, add the following code at the end (you can copy from the complete code, but you need to make some changes, or you can copy the modified code directly from below):
# Import everything needed to edit/save/watch video clipsimport imageioimageio.plugins.ffmpeg.download () from moviepy.editor import VideoFileClipfrom IPython.display import HTML
Ffmpeg.win32.exe, a necessary program for editing, is downloaded here. It is easy to be disconnected during downloading in the private network. You can download it using the download tool and put it in the following path:
C:\ Users\ user name\ AppData\ Local\ imageio\ ffmpeg\ ffmpeg.win32.exedef detect_objects (image_np, sess, detection_graph): # Expand dimensions since the model expects images to have shape: [1, None, None, 3] image_np_expanded = np.expand_dims (image_np, axis=0) image_tensor = detection_graph.get_tensor_by_name ('image_tensor:0') # Each box represents a part of the image where a particular object was detected. Boxes = detection_graph.get_tensor_by_name ('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. Scores = detection_graph.get_tensor_by_name ('detection_scores:0') classes = detection_graph.get_tensor_by_name (' detection_classes:0') num_detections = detection_graph.get_tensor_by_name ('num_detections:0') # Actual detection. (boxes, scores, classes, num_detections) = sess.run ([boxes, scores, classes, num_detections], feed_dict= {image_tensor: image_np_expanded}) # Visualization of the results of a detection. Vis_util.visualize_boxes_and_labels_on_image_array (image_np, np.squeeze (boxes), np.squeeze (classes) .astype (np.int32), np.squeeze (scores), category_index, use_normalized_coordinates=True, line_thickness=8) return image_np
Processing image
Def process_image (image): # NOTE: The output you return should be a color image (3 channel) for processing video below # you should return the final output (image with lines are drawn on lanes) with detection_graph.as_default (): with tf.Session (graph=detection_graph) as sess: image_process = detect_objects (image, sess, detection_graph) return image_process
Input video file
White_output = 'video1_out.mp4'clip1 = VideoFileClip ("video1.mp4"). Subclip (25Magazine 30) white_clip = clip1.fl_image (process_image) # NOTE: this function expects color imagesuploads% time white_clip.write_videofile (white_output, audio=False) where video1.mp4 has been uploaded to the object_detection folder from the computer, and subclip (25pion30) represents the 25-30s period in the identification video.
Original video: show the identified video:
From moviepy.editor import * clip1 = VideoFileClip ("video1_out.mp4") clip1.write_gif ("final.gif")
Import the recognized video to gif format and save it to the object_detection folder.
After reading the above, have you mastered how to use TensorFlow to train and identify objects in video images? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 250
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.