Added collecting signs .py file

parent fec76e3d
File added
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import cv2
import numpy as np
import os
import string
# Create the directory structure
if not os.path.exists("data"):
os.makedirs("data")
if not os.path.exists("data/train"):
os.makedirs("data/train")
if not os.path.exists("data/test"):
os.makedirs("data/test")
for i in range(3):
if not os.path.exists("data/train/" + str(i)):
os.makedirs("data/train/"+str(i))
if not os.path.exists("data/test/" + str(i)):
os.makedirs("data/test/"+str(i))
# Train or test
mode = 'train'
directory = 'data/'+mode+'/'
minValue = 70
cap = cv2.VideoCapture(0)
interrupt = -1
while True:
_, frame = cap.read()
# Simulating mirror image
frame = cv2.flip(frame, 1)
# Getting count of existing images
count = {
'zero': len(os.listdir(directory+"/0")),
'one': len(os.listdir(directory+"/1")),
'two': len(os.listdir(directory+"/2"))
}
# Printing the count in each set to the screen
# cv2.putText(frame, "MODE : "+mode, (10, 50), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,255), 1)
# cv2.putText(frame, "IMAGE COUNT", (10, ), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,255), 1)
cv2.putText(frame, "ZERO : "+str(count['zero']), (10, 70), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,255), 1)
cv2.putText(frame, "ONE : "+str(count['one']), (10, 80), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,255), 1)
cv2.putText(frame, "TWO : "+str(count['two']), (10, 90), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,255), 1)
# Coordinates of the ROI
x1 = int(0.5*frame.shape[1])
y1 = 10
x2 = frame.shape[1]-10
y2 = int(0.5*frame.shape[1])
# Drawing the ROI
# The increment/decrement by 1 is to compensate for the bounding box
cv2.rectangle(frame, (220-1, 9), (620+1, 419), (255,0,0) ,1)
# Extracting the ROI
roi = frame[10:410, 220:520]
# roi = cv2.resize(roi, (64, 64))
cv2.imshow("Frame", frame)
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),2)
# #blur = cv2.bilateralFilter(roi,9,75,75)
th3 = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
ret, test_image = cv2.threshold(th3, minValue, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
#time.sleep(5)
#cv2.imwrite("/home/rc/Downloads/soe/im1.jpg", roi)
#test_image = func("/home/rc/Downloads/soe/im1.jpg")
test_image = cv2.resize(test_image, (300,300))
cv2.imshow("test", test_image)
interrupt = cv2.waitKey(10)
if interrupt & 0xFF == 27: # esc key
break
if interrupt & 0xFF == ord('0'):
cv2.imwrite(directory+'0/'+str(count['zero'])+'.jpg', roi)
if interrupt & 0xFF == ord('1'):
cv2.imwrite(directory+'1/'+str(count['one'])+'.jpg', roi)
if interrupt & 0xFF == ord('2'):
cv2.imwrite(directory+'2/'+str(count['two'])+'.jpg', roi)
cap.release()
cv2.destroyAllWindows()
# In[ ]:
# In[ ]:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment