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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| import math import numpy as np import openpyxl as xl from openpyxl import Workbook import cv2 import time import os import matplotlib.pyplot as plt import plotly.offline as ptly import plotly.graph_objs as go
def get_pointer_rad(img): shape = img.shape c_y, c_x, depth = int(shape[0] / 2), int(shape[1] / 2), shape[2] x1=c_x+c_x*1.5 src = img.copy() freq_list = [] for i in range(361): x = (x1 - c_x) * math.cos(i * math.pi / 180) + c_x y = (x1 - c_x) * math.sin(i * math.pi / 180) + c_y temp = src.copy() cv2.line(temp, (c_x, c_y), (int(x), int(y)), (0, 255, 0), thickness=1) t1 = img.copy() t1[temp[:, :, 1] == 255] = 255 c = img[temp[:, :, 1] == 255] points = c[c == 0] freq_list.append((len(points), i)) cv2.waitKey(1) cv2.destroyAllWindows() return max(freq_list, key=lambda x: x[0])
def getthr(imgc): thres = np.random.randint(40,100) imgfan = cv2.threshold(imgc, thres, 255, cv2.THRESH_BINARY)[1] max = get_pointer_rad(imgfan) thr = max[1] return thr
def get_averg(imgc,h): tol = 0 h = int(h) for i in range(h): thr = getthr(imgc) tol = tol + thr print(i+1,end='、') averg = tol / h return averg
def save_xlsx(thr,cdu,timed): filex = 'ssdata.xlsx' if os.path.exists(filex): print(f'--在ssdata.xlsx中写入数据--') wb = xl.load_workbook('ssdata.xlsx') sheet = wb['Sheet'] else: wb = Workbook() print('--新建一个ssdata.xlsx--') sheet = wb['Sheet']
ws = wb.active ws['A1'] = '指针角度' ws['B1'] = '转换角度' ws['C1'] = '测试时间'
maxrow = sheet.max_row + 1 ws.cell(row=maxrow,column=1,value=thr) ws.cell(row=maxrow,column=2,value=cdu) ws.cell(row=maxrow,column=3,value=timed)
wb.save('ssdata.xlsx')
def get_cts(): filex = 'ssdata.xlsx' if os.path.exists(filex): print(f'--在ssdata.xlsx中写入数据--') wb = xl.load_workbook('ssdata.xlsx') sheet = wb['Sheet'] else: wb = Workbook() print('--新建一个ssdata.xlsx--') sheet = wb['Sheet'] ws = wb.active cdu = [] for row in ws.iter_rows(min_row=2, min_col=2,max_col=2,max_row=sheet.max_row ): for cell in row: cdu.append(str(cell.value)) timed = [] for row in ws.iter_rows(min_row=2, min_col=3,max_col=3,max_row=sheet.max_row ): for cell in row: timed.append(cell.value) return cdu,timed
def get_shishi(cishu,pingjun): imgh = cv2.imread('frame.jpg')
cts = get_cts() cdus = cts[0] timeds = cts[1]
row = True while row: thr = get_averg(imgh,pingjun) if thr > 0 and thr <= 45: cdu = thr / 2.25 + 100 elif thr >= 135 and thr <= 360: cdu = (thr - 135) / 2.25 else: cdu = '测试故障' cdus.append(str(cdu)) timed = time.strftime("%H:%M:%S", time.localtime()) timeds.append(timed)
save_xlsx(thr,cdu,timed)
plt.clf() fig1 = plt.figure(num='温度-时间', figsize=(20, 10), dpi=75, facecolor='#FFFFFF', edgecolor='#0000FF') plt.xlabel('Time') plt.ylabel('Temp') plt.plot(timeds, cdus, 'r-s') plt.pause(0.01)
if len(timeds) >= 22: timeds = [] cdus = [] plt.clf() row = False else : break
def video_show(video,ci): i = 1 while True: ret1,frame = video.read() if not ret1: print("视频获取失败!") break framegray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
template = cv2.imread('./img/moban_c.jpg') theight , twidth = template.shape[0] , template.shape[1] result = cv2.matchTemplate(frame,template,cv2.TM_SQDIFF_NORMED) cv2.normalize(result,result,0,1,cv2.NORM_MINMAX,-1) min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(result)
cv2.rectangle(frame ,min_loc ,(min_loc[0] + twidth ,min_loc[1] + theight) ,(255 ,0 ,0) ,2) cv2.imshow("Video_show",frame)
choose_data = framegray[min_loc[0]:(min_loc[0] + twidth ),min_loc[1]: (min_loc[1] + theight)] cv2.imshow("choose_video",choose_data) if i%ci == 0 : cv2.imwrite(f'frame.jpg',choose_data) print(f'照片已保存--frame.jpg--:') break i += 1 if cv2.waitKey(1) & 0xff == ord("e"): break video.release() cv2.destroyAllWindows()
if __name__ == '__main__' : cishu = 0 pingjun = 5 ci = 100 while True: video = cv2.VideoCapture(0) video_show(video ,ci) get_shishi(cishu ,pingjun)
|