Skip to content

Commit e394959

Browse files
committed
This is the proper working code of final year project commit 1
0 parents  commit e394959

9 files changed

+218
-0
lines changed

000.jpg

4.27 KB
Loading

001.jpg

5.31 KB
Loading

frame302.jpg

614 KB
Loading

image.jpg

597 KB
Loading

image_augmentation.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.gridspec as gridspec
3+
import cv2
4+
5+
import numpy as np
6+
7+
import matplotlib.image as mpimg
8+
def augment_brightness_camera_images(image):
9+
image1 = cv2.cvtColor(image,cv2.COLOR_RGB2HSV)
10+
random_bright = .25+np.random.uniform()
11+
#print(random_bright)
12+
image1[:,:,2] = image1[:,:,2]*random_bright
13+
image1 = cv2.cvtColor(image1,cv2.COLOR_HSV2RGB)
14+
return image1
15+
def transform_image(img,ang_range,shear_range,trans_range,brightness=0):
16+
'''
17+
This function transforms images to generate new images.
18+
The function takes in following arguments,
19+
1- Image
20+
2- ang_range: Range of angles for rotation
21+
3- shear_range: Range of values to apply affine transform to
22+
4- trans_range: Range of values to apply translations over.
23+
24+
A Random uniform distribution is used to generate different parameters for transformation
25+
26+
'''
27+
# Rotation
28+
29+
ang_rot = np.random.uniform(ang_range)-ang_range/2
30+
rows,cols,ch = img.shape
31+
Rot_M = cv2.getRotationMatrix2D((cols/2,rows/2),ang_rot,1)
32+
33+
# Translation
34+
tr_x = trans_range*np.random.uniform()-trans_range/2
35+
tr_y = trans_range*np.random.uniform()-trans_range/2
36+
Trans_M = np.float32([[1,0,tr_x],[0,1,tr_y]])
37+
38+
# Shear
39+
pts1 = np.float32([[5,5],[20,5],[5,20]])
40+
41+
pt1 = 5+shear_range*np.random.uniform()-shear_range/2
42+
pt2 = 20+shear_range*np.random.uniform()-shear_range/2
43+
44+
# Brightness
45+
46+
47+
pts2 = np.float32([[pt1,5],[pt2,pt1],[5,pt2]])
48+
49+
shear_M = cv2.getAffineTransform(pts1,pts2)
50+
51+
img = cv2.warpAffine(img,Rot_M,(cols,rows))
52+
img = cv2.warpAffine(img,Trans_M,(cols,rows))
53+
img = cv2.warpAffine(img,shear_M,(cols,rows))
54+
55+
if brightness == 1:
56+
img = augment_brightness_camera_images(img)
57+
58+
return img
59+
60+
image = cv2.imread('Datasets/stopsign.jpg')
61+
plt.imshow(image);
62+
plt.axis('off');
63+
gs1 = gridspec.GridSpec(10, 10)
64+
gs1.update(wspace=0.01, hspace=0.02) # set the spacing between axes.
65+
plt.figure(figsize=(12,12))
66+
for i in range(100):
67+
ax1 = plt.subplot(gs1[i])
68+
ax1.set_xticklabels([])
69+
ax1.set_yticklabels([])
70+
ax1.set_aspect('equal')
71+
img = transform_image(image,20,10,5,brightness=1)
72+
cv2.imwrite("Augmentation/%.3d.jpg" % i, img)
73+
plt.subplot(10,10,i+1)
74+
plt.imshow(img)
75+
plt.axis('off')
76+
77+
plt.show()

object_detection.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import cv2
2+
from matplotlib import pyplot as plt
3+
img = cv2.imread('target.png')
4+
5+
search = cv2.dilate(img, cv2.getStructuringElement(cv2.MORPH_RECT, (5,5)))
6+
search = cv2.cvtColor(search, cv2.COLOR_BGR2GRAY)
7+
im2, contours, hierarchy = cv2.findContours(search, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
8+
bboxes = [cv2.boundingRect(c) for c in contours]
9+
10+
fig, axes = plt.subplots(1, sum(rect[2]*rect[3] > 250 for rect in bboxes))
11+
fig.set_size_inches([12,3])
12+
fig.tight_layout()
13+
figi = 0
14+
for i in range(len(contours)):
15+
rect = cv2.boundingRect(contours[i])
16+
area = rect[2] * rect[3]
17+
#if area < 250:
18+
if area < 4000:
19+
continue
20+
21+
obj = img[rect[1]:rect[1]+rect[3]+1, rect[0]:rect[0]+rect[2]+1, :]
22+
obj = cv2.cvtColor(obj, cv2.COLOR_BGR2RGB)
23+
imgg = cv2.imread("image.jpg")
24+
crop_img = imgg[rect[1]:rect[1]+rect[3]+1, rect[0]:rect[0]+rect[2]+1]
25+
cv2.imwrite("%.3d.jpg" % figi, crop_img)
26+
#axes[figi].imshow(obj)
27+
cv2.imshow("cropped", crop_img)
28+
#cv2.waitKey(0)
29+
figi += 1
30+
31+
fig.show()
32+
cv2.waitKey(0)

perfectly-working-break-it.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# import cv2;
2+
# katti = cv2.VideoCapture('video1.mp4');
3+
# success,image = katti.read()
4+
# count = 0
5+
# while success:
6+
# cv2.imwrite("evidence/video1/photo%d.jpg"%count,image)
7+
# print('photo %d', count)
8+
# count +=1
9+
10+
11+
# # import cv2
12+
# # import numpy as np
13+
# # import os
14+
15+
# # # Playing video from file:
16+
# # cap = cv2.VideoCapture('video1.mp4')
17+
18+
# # try:
19+
# # if not os.path.exists('data'):
20+
# # os.makedirs('data')
21+
# # except OSError:
22+
# # print ('Error: Creating directory of data')
23+
24+
# # currentFrame = 0
25+
# # while(True):
26+
# # # Capture frame-by-frame
27+
# # ret, frame = cap.read()
28+
29+
# # # Saves image of the current frame in jpg file
30+
# # name = './data/frame' + str(currentFrame) + '.jpg'
31+
# # print ('Creating...' + name)
32+
# # cv2.imwrite(name, frame)
33+
34+
# # # To stop duplicate images
35+
# # currentFrame += 1
36+
37+
# # # When everything done, release the capture
38+
# # cap.release()
39+
# # cv2.destroyAllWindows()
40+
41+
42+
import cv2
43+
vidcap = cv2.VideoCapture('video28.mp4')
44+
success,image = vidcap.read()
45+
count = 0
46+
while success:
47+
cv2.imwrite("Temp/frame%d.jpg" % count, image) # save frame as JPEG file
48+
success,image = vidcap.read()
49+
print('Read a new frame: ', success)
50+
count += 1

red_color_main.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# import cv2
2+
# import numpy as np
3+
4+
# ## Read
5+
# img = cv2.imread("frame0.jpg")
6+
# img = cv2.resize(img, (540, 960))
7+
8+
# ## convert to hsv
9+
# hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
10+
11+
# ## mask of green (36,0,0) ~ (70, 255,255)
12+
# mask1 = cv2.inRange
13+
# #mask1 = cv2.inRange(hsv, (170, 70, 50), (180, 255,255))
14+
# mask1 = cv2.inRange(hsv, (10, 100, 100), (10, 255,255))
15+
# ## mask o yellow (15,0,0) ~ (36, 255, 255)
16+
# #mask2 = cv2.inRange(hsv, (110, 50, 70), (180, 255, 255))
17+
# mask2 = cv2.inRange(hsv, (160, 100, 100), (180, 255, 255))
18+
# ## final mask and masked
19+
# mask = cv2.bitwise_or(mask1, mask2)
20+
# target = cv2.bitwise_and(img,img, mask=mask)
21+
# #bgr_image = cv2.cvtColor(mask1, cv2.COLOR_HSV2BGR)
22+
# cv2.imwrite("target.png", target)
23+
# cv2.imshow('target',target)
24+
# cv2.imshow('mask',mask)
25+
# cv2.imshow('mask1',mask1)
26+
# cv2.imshow('mask2',mask2)
27+
# #cv2.imshow('bgr_image',bgr_image)
28+
# cv2.waitKey(0)
29+
30+
import cv2
31+
import numpy as np
32+
33+
## Read
34+
img = cv2.imread("frame302.jpg")
35+
#img = cv2.resize(img, (540, 960))
36+
37+
## convert to hsv
38+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
39+
40+
## mask of green (36,0,0) ~ (70, 255,255)
41+
#mask1 = cv2.inRange
42+
#mask1 = cv2.inRange(hsv, (170, 70, 50), (180, 255,255))
43+
#mask1 = cv2.inRange(hsv, (-1, 70, 50), (5, 255,255))
44+
mask1 = cv2.inRange(hsv, (-1, 100, 100), (1, 255,255))
45+
## mask o yellow (15,0,0) ~ (36, 255, 255)
46+
#mask2 = cv2.inRange(hsv, (110, 50, 70), (180, 255, 255))
47+
mask2 = cv2.inRange(hsv, (130, 100, 100), (190, 255, 255))
48+
## final mask and masked
49+
mask = cv2.bitwise_or(mask1, mask2)
50+
target = cv2.bitwise_and(img,img, mask=mask)
51+
#bgr_image = cv2.cvtColor(mask1, cv2.COLOR_HSV2BGR)
52+
cv2.imwrite("image.jpg",img)
53+
cv2.imwrite("target.png", target)
54+
cv2.imshow('target',target)
55+
cv2.imshow('mask',mask)
56+
cv2.imshow('mask1',mask1)
57+
cv2.imshow('mask2',mask2)
58+
#cv2.imshow('bgr_image',bgr_image)
59+
cv2.waitKey(0)

target.png

31.5 KB
Loading

0 commit comments

Comments
 (0)