unity3d ai追踪
Using OpenCV, and Haar Cascade
使用OpenCV和Haar级联
Recently, I attended a live-stream of Clever Programmer where I learned how to make small projects using AI, Python, OpenCV, and Haar Cascade. Excited by the live-stream, I was eager to start making some of my own projects using the guidelines that Clever Programmer shared.
最近,我参加了Clever Programmer的直播,学习了如何使用AI,Python,OpenCV和Haar Cascade进行小型项目。 受到现场直播的刺激,我渴望使用Clever Programmer共享的指南开始制作自己的一些项目。
My first attempt at this was to dive into the world of artificial intelligence and see what I could create. At the outset of this endeavor, it felt almost impossible to imagine myself making even a simple app, let alone embed my app with intelligence. However, I was able to put something together; a simple car and pedestrian app which I will guide you through in this article.
我的第一个尝试是进入人工智能世界,看看我能创造什么。 一开始,几乎无法想象自己制作了一个简单的应用程序,更不用说将我的应用程序嵌入智能中了。 但是,我能够将一些东西放在一起。 一个简单的汽车和行人应用程序,本文将为您提供指导。
先决条件和术语 (Prerequisites and Terminology)
Having some basic knowledge of Python is going to be helpful here. Having a basic understanding of artificial intelligence is not required, but you should be familiar with keywords related to artificial intelligence. Here are some helpful definitions:
掌握一些Python的基本知识将对您有所帮助。 不需要对人工智能有基本的了解,但是您应该熟悉与人工智能相关的关键字。 以下是一些有用的定义:
OpenCV (OpenCV)
This is a programming functions library aimed at real-time computer vision.
这是一个针对实时计算机视觉的编程功能库。
- Install OpenCV: run 安装OpenCV:运行
pip install opencv-python
Haar级联 (Haar Cascade)
This is a programming library for object-detection algorithms. It has pre-trained scripts in XML format, which is very efficient in detecting almost any object.
这是一个用于对象检测算法的编程库。 它具有XML格式的预训练脚本,在检测几乎所有对象方面非常有效。
方向: (Directions:)
追踪器创建 (Tracker Creation)
- Import OpenCV as cv2. 将OpenCV导入为cv2。
import cv2
- Save the webcam feed or video as a variable. 将网络摄像头提要或视频另存为变量。
video = cv2.VideoCapture(' # Your mp4 files goes here/ webcam feed goes here')
- Import two Haar cascade files, the link will be given here in the references. 导入两个Haar级联文件,链接将在参考文献中给出。
car_tracker_file = 'cars.xml' # files from library of OpenCV
car_tracker = cv2.CascadeClassifier(car_tracker_file)pedestrian_tracker_file = 'pedestrian.xml' # files from library of OpenCV
pedestrian_tracker = cv2.CascadeClassifier('pedestrian.xml')
- Convert that image to a grayscale image (the computer loves black and white). 将该图像转换为灰度图像(计算机喜欢黑白图像)。
grayscaled_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- Store the grayscale frames. 存储灰度框架。
cars = car_tracker.detectMultiScale(grayscaled_frame)
pedestrians = pedestrian_tracker.detectMultiScale(grayscaled_frame)
- Find the coordinates of the objects. 查找对象的坐标。
for (x, y, w, h) in cars:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
for (x, y, w, h) in pedestrians:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('AI Driven Car Tracker', frame)
- A rectangle will be created around the detected objects. 将在检测到的对象周围创建一个矩形。
- Output the result on the screen. For me it looked something like this: 在屏幕上输出结果。 对我来说,它看起来像这样:
- Release the video frame or image when done. 完成后释放视频帧或图像。
video.release()
These steps might be confusing for most beginners. This is to be expected, as you practice you will gain a familiarity with the process that will help further your understanding of what is going on.
这些步骤可能会使大多数初学者感到困惑。 这是预料之中的,因为您在练习时会熟悉该过程,这将有助于您进一步了解正在发生的事情。
The whole code is here:
整个代码在这里:
import cv2
video = cv2.VideoCapture('# Your video file goes here')
car_tracker_file = 'cars.xml' # files from library of OpenCV
car_tracker = cv2.CascadeClassifier(car_tracker_file)
pedestrian_tracker_file = 'pedestrian.xml' # files from library of OpenCV
pedestrian_tracker = cv2.CascadeClassifier('pedestrian.xml')
while True:
(success, frame) = video.read()
if success:
grayscaled_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
break
cars = car_tracker.detectMultiScale(grayscaled_frame)
pedestrians = pedestrian_tracker.detectMultiScale(grayscaled_frame)
for (x, y, w, h) in cars:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
for (x, y, w, h) in pedestrians:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('AI Driven Car Tracker', frame)
key = cv2.waitKey(1)
if key==81 or key==113:
break
video.release()
使用Python开发AI的可能性似乎无穷无尽 (The Possibilities of Developing AI using Python Seem Endless)
The knowledge and skillset that I was able to develop through creating little apps such as this one have helped me greatly and expanded my capabilities in this realm. As programmers are well aware, AI is a fast field, and, when it comes to what types of programs we can create, the opportunities seem endless.
通过创建像这样的小应用程序,我能够获得的知识和技能集极大地帮助了我,并扩展了我在该领域的能力。 如程序员所知,人工智能是一个快速领域,而当涉及到我们可以创建哪种类型的程序时,机遇似乎是无止境的。
Many of my friends have branched out and begun using AI to create new games. A few are even running their own small blogs on Node.js. I have seen people making automated traders in Python (something I also wanted to try but I am too much of a penny pincher to do this quite yet). Automating is one feature that people especially love to play around with. Occasionally I leave my TV and lights on at night when I go to sleep so I’m planning on making a program that can turn these off when I accidentally forget to do this myself.
我的许多朋友已经分支并开始使用AI来创建新游戏。 一些人甚至在Node.js上运行自己的小型博客。 我见过有人用Python制作自动交易员(我也想尝试一下,但是我还真是一分钱都无法做到这一点)。 自动化是人们特别喜欢玩的功能之一。 有时,晚上入睡时我会打开电视和灯光,因此我计划制作一个程序,当我不小心自己做时可以关闭这些程序。
整理词 (Finishing Words)
I realize that much of the information I shared in this article can be found elsewhere on the web. I’d like to think that I have gone a little deeper here by sharing more in-depth research; breaking things down more than others have done in a way that you will find helpful.
我意识到,我在本文中分享的许多信息都可以在网络上的其他地方找到。 我想通过分享更深入的研究使我在这里变得更深了。 在您会发现有帮助的方式下,将事情分解成比其他事情要多的事情。
The next thing that I’d like to write about is adding a camera feed to the app that can be used while riding. For this, I would like to obtain some hardware and attach this to my bike so as to carry out some alpha testing.
我想写的下一件事是将摄像头供稿添加到可以在骑行时使用的应用程序中。 为此,我想获得一些硬件并将其连接到我的自行车上,以便进行一些Alpha测试。
I have listed the resources that I used to help write this article below. I would love to hear your thoughts on what I have shared, feel free to leave your comments! Thanks for reading.
我在下面列出了用于帮助撰写本文的资源。 我很想听听您对我分享的想法,随时发表您的评论! 谢谢阅读。
资源资源 (Resources)
https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_fullbody.xml
https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_fullbody.xml
https://raw.githubusercontent.com/andrewssobral/vehicle_detection_haarcascades/master/cars.xml
https://raw.githubusercontent.com/andrewssobral/vehicle_detection_haarcascades/master/cars.xml
unity3d ai追踪