1. 일단 opencv로 동영상을 잘라준다
2. yolo 적용해서 object detection 감지 ( 아래 사이트 참조)
* yoloc3.weighs 는 아래에서 다운로드받아서 쓰기
mp4_cctv_list=[]
for i in file_list:
# 현재 디렉토리에 있는 모든 파일 리스트를 가져온다
path = "./task1/"+str(i)
file_listttt = os.listdir(path)
mp4_cctv = [file for file in file_listttt if file.endswith(".mp4")]
mp4_cctv_list.append(mp4_cctv)
for j in range(1,len(file_list)):
mp4_cctv = "./task1/"+str(file_list[j-1])+"/"+str(mp4_cctv_list[j-1][0])
try :
# -------------------- 동영상 쪼개기 ------------------------------------------------------------
import cv2
n=119 #동영상을 119개로 쪼개줄것임
vidcap = cv2.VideoCapture(mp4_cctv)
total_frames = vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
frames_step = total_frames//n
for i in range(n):
#here, we set the parameter 1 which is the frame number to the frame (i*frames_step)
vidcap.set(1,i*frames_step)
success,image = vidcap.read()
#save your image
globals()['col{}.jpg'.format(i)]= image
#cv2.imwrite(globals()['./col{}.jpg'.format(i)],image)
# 저장해줄 위치 지정해줌
cv2.imwrite('./new2/col'+str(i)+'.jpg',image)
vidcap.release()
# -------------------- yolo ------------------------------------------------------------
# Yolo 로드
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))
for k in range(119): #수정 119로
# 이미지 가져오기
print('사진' ,k)
img = cv2.imread("./new2/col"+str(k)+".jpg")
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape
# Detecting objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
# 정보를 화면에 표시
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# 좌표
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
font = cv2.FONT_HERSHEY_PLAIN
label_lists=[]
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
color = colors[i]
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
cv2.putText(img, label, (x, y + 30), font, 3, color, 3)
label_lists.append(label)
cv2.imshow("Image", img)
cv2.waitKey(1)
cv2.destroyAllWindows()
print(label_lists)
df['mp4'][:][k]=label_lists
#맥에서 opencv 안닫힐때 꿀팁
cv2.destroyAllWindows()
cv2.waitKey(1)
cv2.waitKey(1)
cv2.waitKey(1)
cv2.waitKey(1)
except :
print('exept',file_list[j-1],j-1)
'<개념> > Deep learning' 카테고리의 다른 글
Tensorboard 에러날때 (0) | 2022.01.26 |
---|---|
DL 모델 저장후 다시 불러오기(keras, tensorflow) (0) | 2021.08.14 |
[Hierarchical Clustering] 계층적 클러스터링 개념정리 (0) | 2021.05.09 |
베이지안 옵티마이제이션 정리 (Bayesian optimization) (1) | 2021.03.21 |
Coursera Andrew NG - 1 (0) | 2021.02.23 |