-
Notifications
You must be signed in to change notification settings - Fork 5
/
towereye.py
74 lines (57 loc) · 1.95 KB
/
towereye.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import json
import shutil
import cv2
import requests
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
CONFIG_FP = os.path.join(os.path.dirname(__file__), "config.json")
def get_config(config_fp=CONFIG_FP):
with open(config_fp) as f:
config = json.load(f)
return config
def crop_image(x1, y1, x2, y2, image_path, output_path):
# 读取图片
img = cv2.imread(image_path)
# 切割图片
cropped_img = img[y1:y2, x1:x2]
# 保存切割后的图片
cv2.imwrite(output_path, cropped_img)
def is_clear(image_fp, threshold=250):
# 读取图像并转换为灰度图像
image = rgb2gray(plt.imread(image_fp))
# 将图像的数据类型转换为uint8,并将其范围缩放到0-255
image = (image * 255).astype("uint8")
std = image.std()
span = image.max() - image.min()
score = std * span
if score > threshold:
return True
else:
return False
def analysis_visibility(image_fp, threshold=250, tmpdir="./tmp", keep_tmp=False):
config = get_config()
visibles = []
for key, value in config.items():
x1, y1, x2, y2 = value["x1"], value["y1"], value["x2"], value["y2"]
distance = value["distance"]
savefp = os.path.join(tmpdir, f"{key}.jpg")
os.makedirs(tmpdir, exist_ok=True)
crop_image(x1, y1, x2, y2, image_fp, savefp)
if is_clear(savefp, threshold):
visibles.append(distance)
if not visibles:
return 0
if not keep_tmp:
shutil.rmtree(tmpdir)
return round(max(visibles), 0)
if __name__ == "__main__":
image_fp = "./iap-tower-camera.jpg"
resp = requests.get(
"http://view.iap.ac.cn:8080/imageview/northeast.jpg", stream=True
)
if resp.ok:
with open(image_fp, "wb") as f:
f.write(resp.content)
visibility = analysis_visibility(image_fp)
print(f"基于 IAP 铁塔照片反算当前北京市的能见度为 {visibility} km")