예제 #1
0
def main():
    model_name = sys.argv[1]
    if model_name not in ['svm', 'logistic', 'knn']:
        print("Invalid model-name '{}'!".format(model_name))
        return

    print("Using model '{}'...".format(model_name))

    model_serialized_path = get_config(
        "model_{}_serialized_path".format(model_name))
    print("Model deserialized from path '{}'".format(model_serialized_path))

    camera = cv2.VideoCapture(0)

    while True:

        ret, frame = camera.read()
        cv2.imshow("Recording", frame)
        key = cv2.waitKey(1)
        if key == ord('q'):
            break
        elif key == ord('c'):
            if not ret:
                print("Failed to capture image!")
                continue
            frame = resize_image(frame, 400)
            r = cv2.selectROI(frame)
            imCrop = frame[int(r[1]):int(r[1] + r[3]),
                           int(r[0]):int(r[0] + r[2])]
            cv2.imshow("Image", imCrop)
            #cv2.imshow("Webcam recording", frame)
            frame = imCrop
            try:
                frame = apply_image_transformation(frame)
                frame_flattened = frame.flatten()
                classifier_model = joblib.load(model_serialized_path)
                predicted_labels = classifier_model.predict(frame_flattened)
                predicted_label = predicted_labels[0]
                print("Predicted label = {}".format(predicted_label))
                predicted_image = get_image_from_label(predicted_label)
                predicted_image = resize_image(predicted_image, 200)
                cv2.imshow("Prediction = '{}'".format(predicted_label),
                           predicted_image)
                engine = pyttsx.init()
                engine.say("The predicted text is " + str(predicted_label))
                engine.runAndWait()
                engine.stop()
            except Exception:
                exception_traceback = traceback.format_exc()
                print(
                    "Error while applying image transformation with the following exception trace:\n{}"
                    .format(exception_traceback))

    cv2.destroyAllWindows()
    print "The program completed successfully !!"
def main():
    model_name = "svm"
    if model_name not in ['svm', 'logistic', 'knn']:
        print("Invalid model-name '{}'!".format(model_name))
        return

    print("Using model '{}'...".format(model_name))

    model_serialized_path = get_config(
        "model_{}_serialized_path".format(model_name))
    print("Model deserialized from path '{}'".format(model_serialized_path))

    camera = cv2.VideoCapture(0)

    while True:
        ret, frame = camera.read()
        if not ret:
            print("Failed to capture image!")
            continue
        frame = resize_image(frame, 400)
        cv2.imshow("Webcam recording", frame)
        try:
            frame = apply_image_transformation(frame)
            frame_flattened = frame.flatten()
            #print(model_serialized_path)
            #model_serialized_path = "C:\\Users\\AanikaRahman\\Documents\\GitHub\\ASL1\\Sign-Language-Recognition\\data\\generated\\output\\svm\\model-serialized-svm.pkl"
            classifier_model = joblib.load(model_serialized_path)
            #classifier_model = pickle.load(open(model_serialized_path),encoding='latin1')
            predicted_labels = classifier_model.predict(frame_flattened)
            predicted_label = predicted_labels[0]
            print("Predicted label = {}".format(predicted_label))
            predicted_image = get_image_from_label(predicted_label)
            predicted_image = resize_image(predicted_image, 200)
            cv2.imshow("Prediction = '{}'".format(predicted_label),
                       predicted_image)
        except Exception:
            exception_traceback = traceback.format_exc()
            print(
                "Error while applying image transformation with the following exception trace:\n{}"
                .format(exception_traceback))
        cv2.waitKey(2000)
        cv2.destroyAllWindows()
    cv2.destroyAllWindows()
    print("The program completed successfully !!")
def main():
    model_name = sys.argv[1]
    if model_name not in ['svm', 'logistic', 'knn']:
        print("Invalid model-name '{}'!".format(model_name))
        return

    print("Using model '{}'...".format(model_name))

    model_serialized_path = get_config(
        "model_{}_serialized_path".format(model_name))
    print("Model deserialized from path '{}'".format(model_serialized_path))

    camera = cv2.VideoCapture(0)

    while True:
        ret, frame = camera.read()
        if not ret:
            print("Failed to capture image!")
            continue
        frame = resize_image(frame, 400)
        cv2.imshow("Webcam recording", frame)
        try:
            frame = apply_image_transformation(frame)
            frame_flattened = frame.flatten()
            classifier_model = joblib.load(model_serialized_path)
            predicted_labels = classifier_model.predict(frame_flattened)
            predicted_label = predicted_labels[0]
            print("Predicted label = {}".format(predicted_label))
            predicted_image = get_image_from_label(predicted_label)
            predicted_image = resize_image(predicted_image, 200)
            cv2.imshow("Prediction = '{}'".format(predicted_label),
                       predicted_image)
        except Exception:
            exception_traceback = traceback.format_exc()
            print(
                "Error while applying image transformation with the following exception trace:\n{}"
                .format(exception_traceback))
        cv2.waitKey(10000)
        cv2.destroyAllWindows()
    cv2.destroyAllWindows()
    print "The program completed successfully !!"
예제 #4
0
def mainimage():
    font = cv2.FONT_HERSHEY_SIMPLEX
    bottomLeftCornerOfText = (10, 50)
    fontScale = 1
    fontColor = (255, 255, 255)
    lineType = 2
    model_name = "svm"
    if model_name not in ['svm']:
        print("Invalid model-name '{}'!".format(model_name))
        return

    print("Using model '{}'...".format(model_name))

    model_serialized_path = get_config(
        "model_{}_serialized_path".format(model_name))
    print("Model deserialized from path '{}'".format(model_serialized_path))
    classifier_model = joblib.load(model_serialized_path)
    import tkFileDialog
    tk.Tk().withdraw()
    path = tkFileDialog.askopenfile()
    img = cv2.imread(path.name)
    frame = resize_image(img, 400)
    image = frame
    frame = cv2.resize(img, (50, 50))

    try:
        frame = apply_image_transformation(frame)
        frame_flattened = frame.flatten()

        print classifier_model
        predicted_labels = classifier_model.predict(frame_flattened)
        print("Predicted label = {}".format(predicted_labels))
        cv2.putText(image, str(predicted_labels), bottomLeftCornerOfText, font,
                    fontScale, fontColor, lineType)
        cv2.imshow("frame", image)
        key = cv2.waitKey(2)
        if key == 27:
            cv2.destroyAllWindows()
    except Exception:
        exception_traceback = traceback.format_exc()
        print(
            "Error while applying image transformation with the following exception trace:\n{}"
            .format(exception_traceback))
    cv2.waitKey(0)
    print "The program completed successfully !!"
예제 #5
0
vidcap = cv2.VideoCapture(1)
success,frame = vidcap.read()
count = 0
success = True

while success:
  success,frame = vidcap.read()
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  # Display the resulting frame
  cv2.imshow('frame',gray)
  print 'Read a new frame: ', success
  if not success:
            print("Failed to capture image!")
            continue
  frame = resize_image(frame, 400)
  cv2.imshow("Webcam recording", frame)

  # save frames
  cv2.imwrite("frames\\random\\frame%d.jpg" % count, frame)     # save frame as JPEG file
  count += 1

  # PROCESS:
  # extract features
  # make predictions on features
  # output

  if cv2.waitKey(500) & 0xFF == ord('q'):
    break

# When everything done, release the capture