Esempio n. 1
0
    thresh = thresh[y:y + h, x:x + w]
    #contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
    ans = ''

    (_, contours, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
                                        cv2.CHAIN_APPROX_SIMPLE)
    #print("length:{}".format(len(contours)))
    if len(contours) > 0:
        for c in contours:
            if cv2.contourArea(c) > 400:
                #contour = max(contours, key=cv2.contourArea)
                cv2.drawContours(frame, contours, -1, (0, 255, 0), 2)
                #print("Area:{}".format(cv2.contourArea(c)))
                (x1, y1, w1, h1) = cv2.boundingRect(c)
                roi = thresh[y1:y1 + h1, x1:x1 + w1]
                roi = preprocess(roi, 28, 28)
                roi = np.expand_dims(img_to_array(roi), axis=0)
                pred = model.predict(roi)
                pred = np.argmax(pred, axis=1) + 1
                print("Pred:{}".format(str(pred)))
                cv2.putText(frame, str(pred), (x1 - 5, y1 - 5),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
                #cv2.rectangle(frame, (x1, y1), (x1+w1, y1+h1), (0,255,0),2)

    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.imshow('frame', frame)
    cv2.imshow('thresh', thresh)
    if cv2.waitKey(1) == 27:
        break

cv2.destroyAllWindows()
Esempio n. 2
0
import argparse
import cv2
import os

ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True, help="path to input dataset")
ap.add_argument("-m", "--model", required=True, help="path to output model")
args = vars(ap.parse_args())

data = []
labels = []

for imagePath in paths.list_images(args["dataset"]):
    image = cv2.imread(imagePath)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = preprocess(image, 28, 28)
    image = img_to_array(image)
    data.append(image)

    label = imagePath.split(os.path.sep)[-2]
    labels.append(label)

data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)

(trainX, testX, trainY, testY) = train_test_split(data,
                                                  labels,
                                                  test_size=0.25,
                                                  random_state=42)
lb = LabelBinarizer().fit(trainY)
trainY = lb.transform(trainY)
Esempio n. 3
0
ap.add_argument("-d", "--dataset", required=True,
	help="path to the input dataset")
ap.add_argument("-o", "--output", required=True,
	help="path to output model")
args = vars(ap.parse_args())

# init data and labels
data = []
labels = []

# loop over the images in the dataset
for imagePath in paths.list_images(args["dataset"]):
	# load the image, preprocess it and store it in data list
	image = cv2.imread(imagePath)
	image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	image = preprocess(image, width=28, height=28)
	image = img_to_array(image)
	data.append(image)

	# extract label from the image path and append to labels list
	label = imagePath.split(os.path.sep)[-2]
	labels.append(label)

# normalize the data
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)

# split the data into train/test
(trainX, testX, trainY, testY) = train_test_split(data, labels,
	test_size=0.25)
def main(input_digit):
    capture_digit("training_data/" + str(input_digit))
    x, y, w, h = 0, 0, 300, 300
    max_pics = 10
    image_no = 0
    blueUpper = np.array([140,255,255], dtype = "uint8")
    blueLower = np.array([100,50,50], dtype = "uint8")
    cap=cv2.VideoCapture(0)
    if cap.isOpened():
        ret,frame=cap.read()
    else :
        ret = False

    while ret :
        ret,frame=cap.read()
        
        hsv= cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

        # Thresholding Range
        
        
        # # Threshold the HSV image to get only blue colors
        blue=cv2.inRange(hsv,blueLower,blueUpper)
        # Gaussian Blur
        blur = cv2.GaussianBlur(blue, (3, 3), 0)
        ret, thresh = cv2.threshold(blur, 175, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        thresh = thresh[y:y + h, x:x + w]
        #contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
        ans=''
        
        (_, contours, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        #print("length:{}".format(len(contours))) 
        if len(contours) > 0:
            for c in contours:
                if cv2.contourArea(c)>200:
                    #contour = max(contours, key=cv2.contourArea)
                    cv2.drawContours(frame, contours, -1, (0, 255, 0), 2 )
                    #print("Area:{}".format(cv2.contourArea(c)))
                    (x1, y1, w1, h1) = cv2.boundingRect(c)
                    roi = thresh[y1:y1+h1, x1:x1+w1]
                    roi = preprocess(roi, 28, 28)
                    image_no+=1
                    cv2.imwrite("Training_Data/" + str(input_digit)+"/"+str(image_no)+ ".jpg",roi)
                    print("Image Count : {}".format(image_no))
                    
                    
                    
                    
                    
                    cv2.rectangle(frame, (x1, y1), (x1+w1, y1+h1), (0,255,0),2)

        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)            
        cv2.imshow('frame', frame)
        cv2.imshow('thresh', thresh)
        if image_no==1000:
            break
        if cv2.waitKey(1)==27:
            break

    cv2.destroyAllWindows()
    cap.release()
Esempio n. 5
0
                            cv2.CHAIN_APPROX_SIMPLE)[0]
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:4]
    cnts = contours.sort_contours(cnts)[0]

    # init the output image as grayscale with 3 channels
    output = cv2.merge([gray] * 3)
    predictions = []

    # loop over the contours
    for c in cnts:
        # compute the bounding box and extract the digits
        (x, y, w, h) = cv2.boundingRect(c)
        roi = gray[y - 5:y + h + 5, x - 5:x + w + 5]

        # preprocess the roi and classify it
        roi = preprocess(roi, width=28, height=28)
        roi = np.expand_dims(img_to_array(roi), axis=0) / 255.0
        pred = model.predict(roi).argmax(axis=1)[0] + 1
        predictions.append(str(pred))

        # draw prediction on image
        cv2.rectangel(output, (x - 2, y - 2), (x + w + 4, y + h + 4),
                      (0, 255, 0), 1)
        cv2.putText(output, str(pred), (x - 5, y - 5),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 255, 0), 2)

    # show the output image
    print("[INFO] captcha: {}".format("".join(predictions)))
    cv2.imshow("Output", output)
    cv2.waitKey()