using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.IO;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
//demo1();
demo2();
}
static void demo1()
{
List<string> list_images = new List<string>();
DirectoryInfo di = new DirectoryInfo("../../../images/haar/");
FileInfo[] fis = di.GetFiles("*.jpg");
Mat srcImage = new Mat();
foreach (FileInfo fi in fis)
{
list_images.Add(fi.FullName);
}
//initialize the HOG descriptor/person detector
HOGDescriptor hog = new HOGDescriptor();
hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());
Mat orig = new Mat();
foreach (var imagePath in list_images)
{
//load the image and resize it to (1) reduce detection time
// and (2) improve detection accuracy
Mat image = Cv2.ImRead(imagePath);
// detect people in the image
Rect[] rects = hog.DetectMultiScale(image, out double[] foundWeights,
winStride: new Size(4, 4),
padding: new Size(8, 8),
scale: 1.05);
image.CopyTo(orig);
for (int i = 0; i < rects.Length; i++)
{
// draw the original bounding boxes
Cv2.Rectangle(orig,
new Point(rects[i].X, rects[i].Y),
new Point(rects[i].X + rects[i].Width, rects[i].Y + rects[i].Height),
new Scalar(0, 0, 255), 2);
}
Cv2.ImShow("orig", orig);
// OpenCvSharp中没有提供non_max_suppression方法。。。。
List<float> scores = new List<float>();
for (int i = 0; i < foundWeights.Length; i++)
scores.Add((float)foundWeights[i]);
int[] indices;
CvDnn.NMSBoxes(rects, scores, 0.65f, 1, out indices);
List<Rect> pick = new List<Rect>();
for (int i = 0; i < indices.Length; i++)
{
pick.Add(rects[indices[i]]);
}
for (int i = 0; i < pick.Count; i++)
{
Cv2.Rectangle(image,
new Point(pick[i].X, pick[i].Y),
new Point(pick[i].X + pick[i].Width, pick[i].Y + pick[i].Height),
new Scalar(0, 255, 0), 2);
}
Cv2.ImShow("image", image);
if (Cv2.WaitKey(0) == 'q')
break;
}
}
static void demo2()
{
HOGDescriptor hog = new HOGDescriptor();
hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());
VideoCapture cap = new VideoCapture("../../../images/礼让斑马线!齐齐哈尔城市文明的伤!.mp4");
double fps = cap.Get(VideoCaptureProperties.Fps); // 25.0
Console.WriteLine($"Frames per second using video.get(cv2.CAP_PROP_FPS) : {fps}");
double num_frames = cap.Get(VideoCaptureProperties.FrameCount);
Console.WriteLine($"共有 {num_frames} 帧");
double frame_height = cap.Get(VideoCaptureProperties.FrameHeight);
double frame_width = cap.Get(VideoCaptureProperties.FrameWidth);
Console.WriteLine($"高:{frame_height},宽:{frame_width}");
// 跳过多少帧
int skips = 20;
Mat image = new Mat();
Mat orig = new Mat();
while (cap.IsOpened())
{
cap.Read(image);
//判断是否还有没有视频图像
if (image.Empty())
break;
double current = cap.Get(VideoCaptureProperties.PosFrames);
if (current % skips != 0)
continue;
// detect people in the image
Rect[] rects = hog.DetectMultiScale(image, out double[] foundWeights,
winStride: new Size(4, 4),
padding: new Size(8, 8),
scale: 1.05);
image.CopyTo(orig);
for (int i = 0; i < rects.Length; i++)
{
// draw the original bounding boxes
Cv2.Rectangle(orig,
new Point(rects[i].X, rects[i].Y),
new Point(rects[i].X + rects[i].Width, rects[i].Y + rects[i].Height),
new Scalar(0, 0, 255), 2);
}
Cv2.ImShow("orig", orig);
// OpenCvSharp中没有提供 non_max_suppression 方法。。。。
List<float> scores = new List<float>();
for (int i = 0; i < foundWeights.Length; i++)
scores.Add((float)foundWeights[i]);
int[] indices;
CvDnn.NMSBoxes(rects, scores, 0.65f, 1, out indices);
List<Rect> pick = new List<Rect>();
for (int i = 0; i < indices.Length; i++)
{
pick.Add(rects[indices[i]]);
}
for (int i = 0; i < pick.Count; i++)
{
Cv2.Rectangle(image,
new Point(pick[i].X, pick[i].Y),
new Point(pick[i].X + pick[i].Width, pick[i].Y + pick[i].Height),
new Scalar(0, 255, 0), 2);
}
Cv2.ImShow("image", image);
if (Cv2.WaitKey(10) == 'q')
break;
}
}
}
}
C++版本代码如下:
#include <opencv2/opencv.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/cvconfig.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <io.h>
#include <string>
#include <iostream>
#include <ctime>
using namespace cv;
using namespace std;
void demo1()
{
// 获取图片目录
std::string inPath = "..\\images\\haar\\*.jpg";
intptr_t handle;
struct _finddata_t fileinfo;
handle = _findfirst(inPath.c_str(), &fileinfo);
vector<string> list_images;
do {
string Name = fileinfo.name;
list_images.push_back("..\\images\\haar\\" + Name);
} while (!_findnext(handle, &fileinfo));
//initialize the HOG descriptor/person detector
HOGDescriptor hog;
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
for (int i = 0; i < list_images.size(); i++)
{
//load the image and resize it to (1) reduce detection time
// and (2) improve detection accuracy
Mat image = imread(list_images[i]);
vector<Rect> rects;
vector<double> foundWeights;
hog.detectMultiScale(image, rects, foundWeights, 0, Size(4, 4), Size(8, 8), 1.05);
Mat orig;
image.copyTo(orig);
for (int i = 0; i < rects.size(); i++)
{
// draw the original bounding boxes
rectangle(orig,
Point(rects[i].x, rects[i].y),
Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height),
Scalar(0, 0, 255), 2);
}
imshow("orig", orig);
vector<float> scores;
for (int i = 0; i < foundWeights.size(); i++)
scores.push_back(foundWeights[i]);
vector<int> indices;
vector<float> updated_scores;
cv::dnn::softNMSBoxes(rects, scores, updated_scores, 0.65, 0.1, indices);
vector<Rect> pick;
for (int i = 0; i < indices.size(); i++)
{
cout << foundWeights[indices[i]] << endl;
pick.push_back(rects[indices[i]]);
}
// C++ 中没有提供 nonMaximumSuppression 方法
// 网上找的nonMaximumSuppression算法,没啥卵用~~
// https://blog.csdn.net/JulyLi2019/article/details/133353948
for (int i = 0; i < pick.size(); i++)
{
// draw the original bounding boxes
rectangle(image,
Point(pick[i].x, pick[i].y),
Point(pick[i].x + pick[i].width, pick[i].y + pick[i].height),
Scalar(0, 255, 0), 2);
}
imshow("image", image);
if (waitKey(0) == 'q')
break;
}
}
void demo2()
{
HOGDescriptor hog;
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
VideoCapture cap("../images/礼让斑马线!齐齐哈尔城市文明的伤!.mp4");
double fps = cap.get(VideoCaptureProperties::CAP_PROP_FPS); // 25.0
cout << "Frames per second using video.get(cv2.CAP_PROP_FPS) :" << fps << endl;
double num_frames = cap.get(VideoCaptureProperties::CAP_PROP_FRAME_COUNT);
cout << "共有" << num_frames << "帧" << endl;
double frame_height = cap.get(VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT);
double frame_width = cap.get(VideoCaptureProperties::CAP_PROP_FRAME_WIDTH);
cout << "高:" << frame_height << " 宽:" << frame_width << endl;
// 跳过多少帧
int skips = 20.0;
Mat image;
Mat orig;
while (cap.isOpened())
{
cap >> image; //读取当前帧
double current = cap.get(VideoCaptureProperties::CAP_PROP_POS_FRAMES);
if ((int)current % skips != 0.0)
continue;
vector<Rect> rects;
vector<double> foundWeights;
hog.detectMultiScale(image, rects, foundWeights, 0, Size(4, 4), Size(8, 8), 1.05);
Mat orig;
image.copyTo(orig);
for (int i = 0; i < rects.size(); i++)
{
// draw the original bounding boxes
rectangle(orig,
Point(rects[i].x, rects[i].y),
Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height),
Scalar(0, 0, 255), 2);
}
imshow("orig", orig);
vector<float> scores;
for (int i = 0; i < foundWeights.size(); i++)
scores.push_back(foundWeights[i]);
vector<int> indices;
vector<float> updated_scores;
cv::dnn::softNMSBoxes(rects, scores, updated_scores, 0.65, 0.1, indices);
vector<Rect> pick;
for (int i = 0; i < indices.size(); i++)
{
cout << foundWeights[indices[i]] << endl;
pick.push_back(rects[indices[i]]);
}
for (int i = 0; i < pick.size(); i++)
{
// draw the original bounding boxes
rectangle(image,
Point(pick[i].x, pick[i].y),
Point(pick[i].x + pick[i].width, pick[i].y + pick[i].height),
Scalar(0, 255, 0), 2);
}
imshow("image", image);
if (waitKey(10) == 'q')
break;
}
}
int main()
{
// demo1();
demo2(); // 好慢啊...
}
# import the necessary packages
from __future__ import print_function
from imutils.object_detection import non_max_suppression
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to images directory")
args = vars(ap.parse_args())
# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# loop over the image paths
for imagePath in paths.list_images(args["images"]):
# load the image and resize it to (1) reduce detection time
# and (2) improve detection accuracy
image = cv2.imread(imagePath)
image = imutils.resize(image, width=min(400, image.shape[1]))
orig = image.copy()
# detect people in the image
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
padding=(8, 8), scale=1.05)
# 置信度 weights 参数并没有被用起来
# draw the original bounding boxes
for (x, y, w, h) in rects:
cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
# apply non-maxima suppression to the bounding boxes using a
# fairly large overlap threshold to try to maintain overlapping
# boxes that are still people
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
# draw the final bounding boxes
for (xA, yA, xB, yB) in pick:
cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
# show some information on the number of bounding boxes
filename = imagePath[imagePath.rfind("/") + 1:]
print("[INFO] {}: {} original boxes, {} after suppression".format(
filename, len(rects), len(pick)))
# show the output images
cv2.imshow("Before NMS", orig)
cv2.moveWindow('Before NMS', x=0, y=0)
cv2.imshow("After NMS", image)
cv2.imwrite("out.jpg", image)
cv2.moveWindow('After NMS', x=orig.shape[1], y=0)
if cv2.waitKey(0) == ord('q'):
break
demo2:视频识别
# import the necessary packages
from __future__ import print_function
from imutils.object_detection import non_max_suppression
import numpy as np
import argparse
import imutils
import cv2
import time
# construct the argument parse and parse the arguments
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--images", required=True, help="path to images directory")
# args = vars(ap.parse_args())
# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
#
cap = cv2.VideoCapture('videos/礼让斑马线!齐齐哈尔城市文明的伤!.mp4')
# cap = cv2.VideoCapture('../../data/TownCentreXVID.mp4')
fps = cap.get(cv2.CAP_PROP_FPS) # 25.0
print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))
num_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
print('共有', num_frames, '帧') # 共有 2499.0 帧
frame_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
frame_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
print('高:', frame_height, '宽:', frame_width) # 高: 480.0 宽: 640.0
# exit(0)
# 跳过多少帧
skips = 20
# loop over the image paths
# for imagePath in paths.list_images(args["images"]):
while cap.isOpened():
# load the image and resize it to (1) reduce detection time
# and (2) improve detection accuracy
# image = cv2.imread(imagePath)
ret, frame = cap.read()
image = frame
#
current = cap.get(cv2.CAP_PROP_POS_FRAMES)
if current % skips != 0:
continue
image = imutils.resize(image, width=min(400, image.shape[1]))
orig = image.copy()
# detect people in the image
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
padding=(8, 8), scale=1.05)
# draw the original bounding boxes
for (x, y, w, h) in rects:
cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
# apply non-maxima suppression to the bounding boxes using a
# fairly large overlap threshold to try to maintain overlapping
# boxes that are still people
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
# draw the final bounding boxes
for (xA, yA, xB, yB) in pick:
cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
# show some information on the number of bounding boxes
# filename = imagePath[imagePath.rfind("/") + 1:]
# print("[INFO] {}: {} original boxes, {} after suppression".format(
print("[INFO] {} original boxes, {} after suppression".format(len(rects), len(pick)))
# show the output images
cv2.imshow("Before NMS", orig)
cv2.imshow("After NMS", image)
cv2.moveWindow("After NMS", y=0, x=400)
if cv2.waitKey(30) == ord("q"):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.IO;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
//demo1();
demo2();
}
static void demo1()
{
List<string> list_images = new List<string>();
DirectoryInfo di = new DirectoryInfo("../../../images/haar/");
FileInfo[] fis = di.GetFiles("*.jpg");
Mat srcImage = new Mat();
foreach (FileInfo fi in fis)
{
list_images.Add(fi.FullName);
}
//initialize the HOG descriptor/person detector
HOGDescriptor hog = new HOGDescriptor();
hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());
Mat orig = new Mat();
foreach (var imagePath in list_images)
{
//load the image and resize it to (1) reduce detection time
// and (2) improve detection accuracy
Mat image = Cv2.ImRead(imagePath);
// detect people in the image
Rect[] rects = hog.DetectMultiScale(image, out double[] foundWeights,
winStride: new Size(4, 4),
padding: new Size(8, 8),
scale: 1.05);
image.CopyTo(orig);
for (int i = 0; i < rects.Length; i++)
{
// draw the original bounding boxes
Cv2.Rectangle(orig,
new Point(rects[i].X, rects[i].Y),
new Point(rects[i].X + rects[i].Width, rects[i].Y + rects[i].Height),
new Scalar(0, 0, 255), 2);
}
Cv2.ImShow("orig", orig);
// OpenCvSharp中没有提供non_max_suppression方法。。。。
List<float> scores = new List<float>();
for (int i = 0; i < foundWeights.Length; i++)
scores.Add((float)foundWeights[i]);
int[] indices;
CvDnn.NMSBoxes(rects, scores, 0.65f, 1, out indices);
List<Rect> pick = new List<Rect>();
for (int i = 0; i < indices.Length; i++)
{
pick.Add(rects[indices[i]]);
}
for (int i = 0; i < pick.Count; i++)
{
Cv2.Rectangle(image,
new Point(pick[i].X, pick[i].Y),
new Point(pick[i].X + pick[i].Width, pick[i].Y + pick[i].Height),
new Scalar(0, 255, 0), 2);
}
Cv2.ImShow("image", image);
if (Cv2.WaitKey(0) == 'q')
break;
}
}
static void demo2()
{
HOGDescriptor hog = new HOGDescriptor();
hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());
VideoCapture cap = new VideoCapture("../../../images/礼让斑马线!齐齐哈尔城市文明的伤!.mp4");
double fps = cap.Get(VideoCaptureProperties.Fps); // 25.0
Console.WriteLine($"Frames per second using video.get(cv2.CAP_PROP_FPS) : {fps}");
double num_frames = cap.Get(VideoCaptureProperties.FrameCount);
Console.WriteLine($"共有 {num_frames} 帧");
double frame_height = cap.Get(VideoCaptureProperties.FrameHeight);
double frame_width = cap.Get(VideoCaptureProperties.FrameWidth);
Console.WriteLine($"高:{frame_height},宽:{frame_width}");
// 跳过多少帧
int skips = 20;
Mat image = new Mat();
Mat orig = new Mat();
while (cap.IsOpened())
{
cap.Read(image);
//判断是否还有没有视频图像
if (image.Empty())
break;
double current = cap.Get(VideoCaptureProperties.PosFrames);
if (current % skips != 0)
continue;
// detect people in the image
Rect[] rects = hog.DetectMultiScale(image, out double[] foundWeights,
winStride: new Size(4, 4),
padding: new Size(8, 8),
scale: 1.05);
image.CopyTo(orig);
for (int i = 0; i < rects.Length; i++)
{
// draw the original bounding boxes
Cv2.Rectangle(orig,
new Point(rects[i].X, rects[i].Y),
new Point(rects[i].X + rects[i].Width, rects[i].Y + rects[i].Height),
new Scalar(0, 0, 255), 2);
}
Cv2.ImShow("orig", orig);
// OpenCvSharp中没有提供 non_max_suppression 方法。。。。
List<float> scores = new List<float>();
for (int i = 0; i < foundWeights.Length; i++)
scores.Add((float)foundWeights[i]);
int[] indices;
CvDnn.NMSBoxes(rects, scores, 0.65f, 1, out indices);
List<Rect> pick = new List<Rect>();
for (int i = 0; i < indices.Length; i++)
{
pick.Add(rects[indices[i]]);
}
for (int i = 0; i < pick.Count; i++)
{
Cv2.Rectangle(image,
new Point(pick[i].X, pick[i].Y),
new Point(pick[i].X + pick[i].Width, pick[i].Y + pick[i].Height),
new Scalar(0, 255, 0), 2);
}
Cv2.ImShow("image", image);
if (Cv2.WaitKey(10) == 'q')
break;
}
}
}
}
C++版本代码如下:
#include <opencv2/opencv.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/cvconfig.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <io.h>
#include <string>
#include <iostream>
#include <ctime>
using namespace cv;
using namespace std;
void demo1()
{
// 获取图片目录
std::string inPath = "..\\images\\haar\\*.jpg";
intptr_t handle;
struct _finddata_t fileinfo;
handle = _findfirst(inPath.c_str(), &fileinfo);
vector<string> list_images;
do {
string Name = fileinfo.name;
list_images.push_back("..\\images\\haar\\" + Name);
} while (!_findnext(handle, &fileinfo));
//initialize the HOG descriptor/person detector
HOGDescriptor hog;
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
for (int i = 0; i < list_images.size(); i++)
{
//load the image and resize it to (1) reduce detection time
// and (2) improve detection accuracy
Mat image = imread(list_images[i]);
vector<Rect> rects;
vector<double> foundWeights;
hog.detectMultiScale(image, rects, foundWeights, 0, Size(4, 4), Size(8, 8), 1.05);
Mat orig;
image.copyTo(orig);
for (int i = 0; i < rects.size(); i++)
{
// draw the original bounding boxes
rectangle(orig,
Point(rects[i].x, rects[i].y),
Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height),
Scalar(0, 0, 255), 2);
}
imshow("orig", orig);
vector<float> scores;
for (int i = 0; i < foundWeights.size(); i++)
scores.push_back(foundWeights[i]);
vector<int> indices;
vector<float> updated_scores;
cv::dnn::softNMSBoxes(rects, scores, updated_scores, 0.65, 0.1, indices);
vector<Rect> pick;
for (int i = 0; i < indices.size(); i++)
{
cout << foundWeights[indices[i]] << endl;
pick.push_back(rects[indices[i]]);
}
// C++ 中没有提供 nonMaximumSuppression 方法
// 网上找的nonMaximumSuppression算法,没啥卵用~~
// https://blog.csdn.net/JulyLi2019/article/details/133353948
for (int i = 0; i < pick.size(); i++)
{
// draw the original bounding boxes
rectangle(image,
Point(pick[i].x, pick[i].y),
Point(pick[i].x + pick[i].width, pick[i].y + pick[i].height),
Scalar(0, 255, 0), 2);
}
imshow("image", image);
if (waitKey(0) == 'q')
break;
}
}
void demo2()
{
HOGDescriptor hog;
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
VideoCapture cap("../images/礼让斑马线!齐齐哈尔城市文明的伤!.mp4");
double fps = cap.get(VideoCaptureProperties::CAP_PROP_FPS); // 25.0
cout << "Frames per second using video.get(cv2.CAP_PROP_FPS) :" << fps << endl;
double num_frames = cap.get(VideoCaptureProperties::CAP_PROP_FRAME_COUNT);
cout << "共有" << num_frames << "帧" << endl;
double frame_height = cap.get(VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT);
double frame_width = cap.get(VideoCaptureProperties::CAP_PROP_FRAME_WIDTH);
cout << "高:" << frame_height << " 宽:" << frame_width << endl;
// 跳过多少帧
int skips = 20.0;
Mat image;
Mat orig;
while (cap.isOpened())
{
cap >> image; //读取当前帧
double current = cap.get(VideoCaptureProperties::CAP_PROP_POS_FRAMES);
if ((int)current % skips != 0.0)
continue;
vector<Rect> rects;
vector<double> foundWeights;
hog.detectMultiScale(image, rects, foundWeights, 0, Size(4, 4), Size(8, 8), 1.05);
Mat orig;
image.copyTo(orig);
for (int i = 0; i < rects.size(); i++)
{
// draw the original bounding boxes
rectangle(orig,
Point(rects[i].x, rects[i].y),
Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height),
Scalar(0, 0, 255), 2);
}
imshow("orig", orig);
vector<float> scores;
for (int i = 0; i < foundWeights.size(); i++)
scores.push_back(foundWeights[i]);
vector<int> indices;
vector<float> updated_scores;
cv::dnn::softNMSBoxes(rects, scores, updated_scores, 0.65, 0.1, indices);
vector<Rect> pick;
for (int i = 0; i < indices.size(); i++)
{
cout << foundWeights[indices[i]] << endl;
pick.push_back(rects[indices[i]]);
}
for (int i = 0; i < pick.size(); i++)
{
// draw the original bounding boxes
rectangle(image,
Point(pick[i].x, pick[i].y),
Point(pick[i].x + pick[i].width, pick[i].y + pick[i].height),
Scalar(0, 255, 0), 2);
}
imshow("image", image);
if (waitKey(10) == 'q')
break;
}
}
int main()
{
// demo1();
demo2(); // 好慢啊...
}
# import the necessary packages
from __future__ import print_function
from imutils.object_detection import non_max_suppression
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to images directory")
args = vars(ap.parse_args())
# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# loop over the image paths
for imagePath in paths.list_images(args["images"]):
# load the image and resize it to (1) reduce detection time
# and (2) improve detection accuracy
image = cv2.imread(imagePath)
image = imutils.resize(image, width=min(400, image.shape[1]))
orig = image.copy()
# detect people in the image
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
padding=(8, 8), scale=1.05)
# 置信度 weights 参数并没有被用起来
# draw the original bounding boxes
for (x, y, w, h) in rects:
cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
# apply non-maxima suppression to the bounding boxes using a
# fairly large overlap threshold to try to maintain overlapping
# boxes that are still people
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
# draw the final bounding boxes
for (xA, yA, xB, yB) in pick:
cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
# show some information on the number of bounding boxes
filename = imagePath[imagePath.rfind("/") + 1:]
print("[INFO] {}: {} original boxes, {} after suppression".format(
filename, len(rects), len(pick)))
# show the output images
cv2.imshow("Before NMS", orig)
cv2.moveWindow('Before NMS', x=0, y=0)
cv2.imshow("After NMS", image)
cv2.imwrite("out.jpg", image)
cv2.moveWindow('After NMS', x=orig.shape[1], y=0)
if cv2.waitKey(0) == ord('q'):
break
demo2:视频识别
# import the necessary packages
from __future__ import print_function
from imutils.object_detection import non_max_suppression
import numpy as np
import argparse
import imutils
import cv2
import time
# construct the argument parse and parse the arguments
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--images", required=True, help="path to images directory")
# args = vars(ap.parse_args())
# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
#
cap = cv2.VideoCapture('videos/礼让斑马线!齐齐哈尔城市文明的伤!.mp4')
# cap = cv2.VideoCapture('../../data/TownCentreXVID.mp4')
fps = cap.get(cv2.CAP_PROP_FPS) # 25.0
print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))
num_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
print('共有', num_frames, '帧') # 共有 2499.0 帧
frame_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
frame_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
print('高:', frame_height, '宽:', frame_width) # 高: 480.0 宽: 640.0
# exit(0)
# 跳过多少帧
skips = 20
# loop over the image paths
# for imagePath in paths.list_images(args["images"]):
while cap.isOpened():
# load the image and resize it to (1) reduce detection time
# and (2) improve detection accuracy
# image = cv2.imread(imagePath)
ret, frame = cap.read()
image = frame
#
current = cap.get(cv2.CAP_PROP_POS_FRAMES)
if current % skips != 0:
continue
image = imutils.resize(image, width=min(400, image.shape[1]))
orig = image.copy()
# detect people in the image
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
padding=(8, 8), scale=1.05)
# draw the original bounding boxes
for (x, y, w, h) in rects:
cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
# apply non-maxima suppression to the bounding boxes using a
# fairly large overlap threshold to try to maintain overlapping
# boxes that are still people
rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)
# draw the final bounding boxes
for (xA, yA, xB, yB) in pick:
cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
# show some information on the number of bounding boxes
# filename = imagePath[imagePath.rfind("/") + 1:]
# print("[INFO] {}: {} original boxes, {} after suppression".format(
print("[INFO] {} original boxes, {} after suppression".format(len(rects), len(pick)))
# show the output images
cv2.imshow("Before NMS", orig)
cv2.imshow("After NMS", image)
cv2.moveWindow("After NMS", y=0, x=400)
if cv2.waitKey(30) == ord("q"):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()