-
Notifications
You must be signed in to change notification settings - Fork 0
/
ship.py
62 lines (48 loc) · 1.38 KB
/
ship.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
# This file is used to detect the ship in a video.
import numpy as np
import cv2
ship_cascade = cv2.CascadeClassifier('cascade.xml')
cap = cv2.VideoCapture('sample8.mpg')
xa=[]
ya=[]
ctr=0
medx=0
medy=0
numx=0
numy=0
while True:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ships = ship_cascade.detectMultiScale(gray,20,20)
# print('average:'+str(medx)+' '+str(medy))
# ctr+=1
# if ctr == 60:
# xa.sort()
# ya.sort()
# numx=len(xa)
# numy=len(ya)
# if numx%2==0:
# medx=(xa[int(numx/2)] + xa[int(numx/2 +1)] )/2
# else:
# medx= xa[int((numx+1)/2)]
# if numy%2==0:
# medy=( ya[int(numy/2)] + ya[int(numx/2 +1)] )/2
# else:
# medy=ya[int((numy+1)/2)]
# xa[:]=[]
# ya[:]=[]
for (x,y,w,h) in ships:
xa.append(x)
ya.append(y)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
print('x:'+str(x)+'y:'+str(y))
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
# cv2.rectangle(img,(0,0),(20,20),(0,0,0),2)
# cv2.rectangle(img,(int(medx),int(medy)),(int(medx+20),int(medy+20)),(0,0,0),2)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()