1637814968
Trong hướng dẫn này, chúng ta sẽ học cách sử dụng Theo dõi đối tượng với Opencv và Python.
Trước hết, phải rõ ràng rằng sự khác biệt giữa phát hiện đối tượng và theo dõi đối tượng là gì:
Phát hiện đối tượng là phát hiện trên từng khung hình và khung hình này đến khung hình khác.
Theo dõi đối tượng thực hiện theo dõi từng khung hình nhưng vẫn giữ lịch sử vị trí của đối tượng vào từng thời điểm
Đầu tiên chúng ta sẽ nói về phát hiện đối tượng và sau đó là về cách áp dụng theo dõi đối tượng để phát hiện.
Các ứng dụng khả thi khác nhau, chẳng hạn như đếm có bao nhiêu người ở một khu vực nhất định, kiểm tra xem có bao nhiêu đối tượng đi qua trên băng chuyền hoặc đếm phương tiện trên đường cao tốc.
Chắc chắn khi xem bài hướng dẫn bạn sẽ dễ dàng liên tưởng đến hàng nghìn ý tưởng áp dụng vào thực tế cuộc sống hoặc tiềm năng cho ngành công nghiệp. Chắc chắn, nếu bạn cần thiết kế ba phần của các đối tượng thì đây là công cụ bạn cần.
Trong hướng dẫn này, chúng tôi sẽ sử dụng 3 tệp:
1 Video về đường cao tốc chúng tôi sẽ sử dụng để đếm các phương tiện
2 tệp trình theo dõi. Điều này đã được viết và bạn chỉ cần tải xuống tệp chính
3 . Viết thư cho tôi trong thời gian thực và chúng tôi sẽ tiến hành từng bước với việc tích hợp các thư viện
Đầu tiên, chúng ta cần gọi tệp Highway.mp4 và tạo một mặt nạ
cap = cv2.VideoCapture("highway.mp4")
# Object detection from Stable camera
object_detector = cv2.createBackgroundSubtractorMOG2()
while True:
ret, frame = cap.read()
# 1. Object Detection
mask = object_detector.apply(frame)
Như bạn có thể thấy trong mã ví dụ, chúng tôi cũng đã sử dụng hàm createBackgroundSubtractorMOG2 Trả về tham số "tỷ lệ nền" của thuật toán và sau đó tạo mặt nạ.
Đây là kết quả đầu tiên:
Tuy nhiên, như bạn có thể thấy, có rất nhiều nhiễu trong hình ảnh. Vì vậy, hãy cải thiện việc trích xuất bằng cách loại bỏ tất cả các phần tử nhỏ hơn và tập trung sự chú ý vào các đối tượng lớn hơn một khu vực nhất định.
_, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
# Calculate area and remove small elements
area = cv2.contourArea(cnt)
if area > 100:
#Show image
Vẽ các đường bao bằng hàm cv2.drawContours của OpenCV, chúng ta thu được kết quả này. Bạn sẽ không cần sử dụng chức năng này, hãy coi nó như một bản gỡ lỗi của kết quả đầu tiên
Với mục đích của hướng dẫn này, việc phân tích toàn bộ cửa sổ không quan trọng. Chúng tôi chỉ quan tâm đến việc đếm tất cả các phương tiện đi qua tại một điểm nhất định, vì lý do này, chúng tôi phải xác định khu vực quan tâm ROI và chỉ áp dụng mặt nạ trong khu vực này.
while True:
ret, frame = cap.read()
height, width, _ = frame.shape
# Extract Region of interest
roi = frame[340: 720,500: 800]
# 1. Object Detection
mask = object_detector.apply(roi)
_, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
# Calculate area and remove small elements
area = cv2.contourArea(cnt)
if area > 100:
cv2.drawContours(roi, [cnt], -1, (0, 255, 0), 2)
Đã có trong hình ảnh, bạn có thể thấy một kết quả đầu tiên tốt.
Hàm cv2.createBackgroundSubtractorMOG2 đã được thêm vào lúc đầu mà không xác định tham số, bây giờ chúng ta hãy xem cách cải thiện kết quả của chúng ta. history là tham số đầu tiên, trong trường hợp này, nó được đặt thành 100 vì máy ảnh được cố định. var Threshold thay vào đó là 40 vì giá trị càng thấp thì khả năng xác định sai càng cao. Trong trường hợp này, chúng tôi chỉ quan tâm đến các đối tượng lớn hơn.
# Object detection from Stable camera
object_detector = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=40)
Trước khi tiếp tục với hình chữ nhật, chúng tôi làm sạch thêm hình ảnh. Để làm điều này, chức năng ngưỡng rất hữu ích. Bắt đầu từ mặt nạ của chúng tôi, chúng tôi nói với nó rằng chúng tôi muốn chỉ hiển thị các giá trị trắng hoặc đen, vì vậy bằng cách viết “254, 255” chỉ các giá trị từ 254 đến 255 sẽ được xem xét.
_, mask = cv2.threshold(mask, 254, 255, cv2.THRESH_BINARY)
Sau đó, chúng tôi chèn tọa độ của đối tượng tìm thấy vào điều kiện if và vẽ hình chữ nhật
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 3)
đây là kết quả cuối cùng
Như bạn có thể thấy, chúng tôi có mọi thứ bạn cần để tiến hành theo dõi đối tượng.
Bây giờ chúng ta chỉ cần nhập và tích hợp các chức năng theo dõi.
from tracker import *
# Create tracker object
tracker = EuclideanDistTracker()
Khi đối tượng đã được tạo, do đó chúng ta phải lấy từng vị trí của hộp giới hạn và chèn chúng vào một mảng duy nhất.
detections.append([x, y, w, h])
Bằng cách hiển thị kết quả trên màn hình, bạn có thể thấy tất cả các làn đi qua ROI của chúng tôi được xác định như thế nào và vị trí của chúng được chèn vào một mảng cụ thể. Rõ ràng, càng nhiều xe máy được xác định thì mảng của chúng tôi sẽ càng lớn
Bây giờ hãy chuyển mảng của chúng ta với các vị trí cho tracker.update () . Chúng ta sẽ lại nhận được một mảng với các potions nhưng ngoài ra, một id duy nhất sẽ được chỉ định cho mỗi đối tượng.
Như bạn có thể thấy từ đoạn mã, chúng tôi có thể phân tích mọi thứ bằng vòng lặp for. Tại thời điểm này, chúng ta chỉ cần vẽ hình chữ nhật và hiển thị ID xe.
# 2. Object Tracking
boxes_ids = tracker.update(detections)
for box_id in boxes_ids:
x, y, w, h, id = box_id
cv2.putText(roi, str(id), (x, y - 15), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2)
cv2.rectangle(roi, (x, y), (x + w, y + h), (0, 255, 0), 3)
Trong hình ảnh, bạn có thể thấy kết quả
Như bạn cũng có thể thấy từ video, chúng tôi đã thu được kết quả mà chúng tôi đã tự đặt ở phần đầu của hướng dẫn này.
Tuy nhiên, bạn phải coi nó như một bài tập hoặc điểm khởi đầu vì về chủ đề này có rất nhiều điều để nói và mục đích của hướng dẫn này chỉ là làm cho bạn hiểu nguyên tắc theo dõi đối tượng.
Nếu bạn muốn tích hợp Theo dõi đối tượng vào dự án của mình, bạn nên sử dụng các phương pháp phát hiện đối tượng nâng cao và đáng tin cậy hơn, cũng như các phương pháp theo dõ
1619510796
Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.
Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is
Syntax: x = lambda arguments : expression
Now i will show you some python lambda function examples:
#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map
1626775355
No programming language is pretty much as diverse as Python. It enables building cutting edge applications effortlessly. Developers are as yet investigating the full capability of end-to-end Python development services in various areas.
By areas, we mean FinTech, HealthTech, InsureTech, Cybersecurity, and that's just the beginning. These are New Economy areas, and Python has the ability to serve every one of them. The vast majority of them require massive computational abilities. Python's code is dynamic and powerful - equipped for taking care of the heavy traffic and substantial algorithmic capacities.
Programming advancement is multidimensional today. Endeavor programming requires an intelligent application with AI and ML capacities. Shopper based applications require information examination to convey a superior client experience. Netflix, Trello, and Amazon are genuine instances of such applications. Python assists with building them effortlessly.
Python can do such numerous things that developers can't discover enough reasons to admire it. Python application development isn't restricted to web and enterprise applications. It is exceptionally adaptable and superb for a wide range of uses.
Robust frameworks
Python is known for its tools and frameworks. There's a structure for everything. Django is helpful for building web applications, venture applications, logical applications, and mathematical processing. Flask is another web improvement framework with no conditions.
Web2Py, CherryPy, and Falcon offer incredible capabilities to customize Python development services. A large portion of them are open-source frameworks that allow quick turn of events.
Simple to read and compose
Python has an improved sentence structure - one that is like the English language. New engineers for Python can undoubtedly understand where they stand in the development process. The simplicity of composing allows quick application building.
The motivation behind building Python, as said by its maker Guido Van Rossum, was to empower even beginner engineers to comprehend the programming language. The simple coding likewise permits developers to roll out speedy improvements without getting confused by pointless subtleties.
Utilized by the best
Alright - Python isn't simply one more programming language. It should have something, which is the reason the business giants use it. Furthermore, that too for different purposes. Developers at Google use Python to assemble framework organization systems, parallel information pusher, code audit, testing and QA, and substantially more. Netflix utilizes Python web development services for its recommendation algorithm and media player.
Massive community support
Python has a steadily developing community that offers enormous help. From amateurs to specialists, there's everybody. There are a lot of instructional exercises, documentation, and guides accessible for Python web development solutions.
Today, numerous universities start with Python, adding to the quantity of individuals in the community. Frequently, Python designers team up on various tasks and help each other with algorithmic, utilitarian, and application critical thinking.
Progressive applications
Python is the greatest supporter of data science, Machine Learning, and Artificial Intelligence at any enterprise software development company. Its utilization cases in cutting edge applications are the most compelling motivation for its prosperity. Python is the second most well known tool after R for data analytics.
The simplicity of getting sorted out, overseeing, and visualizing information through unique libraries makes it ideal for data based applications. TensorFlow for neural networks and OpenCV for computer vision are two of Python's most well known use cases for Machine learning applications.
Thinking about the advances in programming and innovation, Python is a YES for an assorted scope of utilizations. Game development, web application development services, GUI advancement, ML and AI improvement, Enterprise and customer applications - every one of them uses Python to its full potential.
The disadvantages of Python web improvement arrangements are regularly disregarded by developers and organizations because of the advantages it gives. They focus on quality over speed and performance over blunders. That is the reason it's a good idea to utilize Python for building the applications of the future.
#python development services #python development company #python app development #python development #python in web development #python software development
1591743681
Learn Free how to create a virtual pen and eraser with python and OpenCV with source code and complete guide. This entire application is built fundamentally on contour detection. It can be thought of as something like closed color curves on compromises that have the same color or intensity, it’s like a blob. In this project we use color masking to get the binary mask of our target color pen, then we use the counter detection to find the location of this pen and the contour to find it.
#python #create virtual pen and eraser with opencv #create virtual pen and eraser with python opencv #programming #opencv #python opencv
1602968400
Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.
Swapping value in Python
Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead
>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName
>>> print(FirstName, LastName)
('Jordan', 'kalebu')
#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development
1602666000
Today you’re going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates.
In many situations you may find yourself having duplicates files on your disk and but when it comes to tracking and checking them manually it can tedious.
Heres a solution
Instead of tracking throughout your disk to see if there is a duplicate, you can automate the process using coding, by writing a program to recursively track through the disk and remove all the found duplicates and that’s what this article is about.
But How do we do it?
If we were to read the whole file and then compare it to the rest of the files recursively through the given directory it will take a very long time, then how do we do it?
The answer is hashing, with hashing can generate a given string of letters and numbers which act as the identity of a given file and if we find any other file with the same identity we gonna delete it.
There’s a variety of hashing algorithms out there such as
#python-programming #python-tutorials #learn-python #python-project #python3 #python #python-skills #python-tips