特征匹配

1
2
pip install opencv-python
pip install opencv-contrib-python==3.4.2.17

这里一定要加版本号,负责会报一个版权错误
代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# import numpy as np
import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread('a1.jpg', 0) # queryImage
img2 = cv2.imread('a4.jpg', 0) # trainImage

# Initiate SIFT detector
sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)

# FLANN parameters
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50) # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params, search_params)

matches = flann.knnMatch(des1, des2, k=2)
# print(matches)

# Need to draw only good matches, so create a mask
matchesMask = [[0, 0] for i in range(len(matches))]

# ratio test as per Lowe's paper
for i, (m, n) in enumerate(matches):
if m.distance < 0.7*n.distance:
matchesMask[i] = [1, 0]

draw_params = dict(matchColor=(0, 255, 0),
singlePointColor=(255, 0, 0),
matchesMask=matchesMask,
flags=0)

img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches, None, **draw_params)

plt.imshow(img3,), plt.show()

pil和cv2图片互转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import cv2
import numpy as np
from PIL import Image

cap = cv2.VideoCapture(0)
img2 = Image.open("./1.jpg")

while(True):
ret, frame = cap.read()
if ret is False:
cv2.waitKey()
break

cv2img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

pilimg = Image.fromarray(cv2img)
pilimg.paste(img2, (200, 200))

cv2img = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
cv2.imshow('frame', cv2img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

支持H264

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import cv2
import numpy as np
from PIL import Image

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'avc1')
out = cv2.VideoWriter('./output.mp4', fourcc, 30.0, (640, 480), True)

img2 = Image.open("./1.jpg")

while(True):
ret, frame = cap.read()
if ret is False:
cv2.waitKey()
break

cv2img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

pilimg = Image.fromarray(cv2img)
pilimg.paste(img2, (200, 200))

cv2img = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)
cv2.imshow('frame', cv2img)

out.write(cv2img)
if cv2.waitKey(1) & 0xFF == ord('q'):

break

cap.release()
out.release()
cv2.destroyAllWindows()

当提示不支持h264时候可以下载对应版本的openh264-1.7.0-win64.dll,然后放在python的安装目录即可

下载文件

1
2
3
4
5
6
import urllib.request

url = "https://baidu.com"
path = "./"
x = "index.html"
urllib.request.urlretrieve(url, '{0}{1}'.format(path, x))

ffmpeg

api https://kkroening.github.io/ffmpeg-python/
源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pip install ffmpeg-python==0.1.1

import ffmpeg
def combine(video_path, audio_path, out_path):
video = ffmpeg.input(video_path)
audio = ffmpeg.input(audio_path)
out = ffmpeg.output(
audio,
video,
out_path,
vcodec='copy',
acodec='copy',
threads='5'
)
ffmpeg.overwrite_output(out).run()

本地安装依赖

1
2
3
4
5
6
requirements.txt
存放依赖关系
例如
ffmpeg-python==0.2.0

pip install -t . -r requirements.txt -U

多线程,队列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from queue import PriorityQueue
import threading

def run(n):
print("task ", n, 'start')
time.sleep(n)
print("task ", n, 'finish')


start_time = time.time()
t1 = threading.Thread(target=run, args=(1,))
t2 = threading.Thread(target=run, args=(4,))

t1.start()
t2.start()

t2.join()
t1.join()

print("cost :", time.time()-start_time)

---

Q = PriorityQueue()
Q.put((i, frame))
for i in range(0, Q.qsize()):
frame = Q.get()[1]

打包

1
2
3
4
5
pip install pyinstaller
程序目录
pyinstaller -F test.py
centos打包
https://www.cnblogs.com/jerryzh/p/10937905.html

阿里云源

1
pip3 命令最后加上 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

获取当前目录

1
2
3
4
5
6
7
8
# 打包后路径错误
print(sys.path[0])
# 当前文件地址
print(sys.argv[0])
# Python或打包后名字
print(os.path.dirname(os.path.realpath(sys.executable)))
# 文件目录
print(os.path.dirname(os.path.realpath(sys.argv[0])))