import cv2 as cv import numpy as np import time cap = cv.VideoCapture() r = 0 while r <=100: cap.open('http://**.**.***.***:***/*****CG?container=mjpeg&stream=main') #URL-адрес видеопотока hasFrame, frame = cap.read()#Чтение кадра из потока frame = frame[100:200, 300:750] box = [0,335,100,385] quantframe = frame[box[0]:box[2], box[1]:box[3]]#Сохранение в отдельную переменную части кадра с изображением машины r+=1 cv.imwrite(str(r)+'.png',quantframe) #Сохранение изображения машины в файл print('saved') cap.release() time.sleep(3600) key = cv.waitKey(1) if key & 0xFF == ord('q'): cv.destroyAllWindows() break
def contoursfinder(image): img = image.copy() srcimg = img.copy() hsv_min = np.array((0, 0, 0), np.uint8) hsv_max = np.array((255, 255, 60), np.uint8) #Поскольку контуры находятся по различиям в цвете между частями картинки, необходимо подобрать параметры, исходя из цветовой гаммы картинки hsv = cv.cvtColor( img, cv.COLOR_BGR2HSV ) thresh = cv.inRange( hsv, hsv_min, hsv_max ) contours, _ = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) img = np.zeros((100,50,3), np.uint8) cv.drawContours(img, contours, -1, (255,255,255), 1, cv.LINE_AA) return cv.cvtColor(img, cv.COLOR_BGR2GRAY)
model = keras.Sequential([ keras.layers.Flatten(input_shape=(100, 50)), keras.layers.Dense(256, activation=tf.nn.relu), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(2, activation=tf.nn.softmax) ]) model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(trainingimagesarr, trainingimagesarrlabel, epochs=1, callbacks=[tbCallBack])
trainingimagesarr = trainingimagesarr / 255.0 trainingimagesarrlabel = np.array(trainingimagesarrlabel)-1
def realtest(): cap = cv.VideoCapture() cap.open('http://**.**.***.***:***/*****CG?container=mjpeg&stream=main') hasFrame, frame = cap.read() frame = frame[100:200, 300:750] quantframe = frame[0:100,275:325] quantframe = contoursfinder(quantframe) return model.predict(np.array([quantframe]))[0][1]>0.60
Source: https://habr.com/ru/post/440608/