Skip to content
Snippets Groups Projects
Commit ec194c86 authored by Michiel_VE's avatar Michiel_VE
Browse files

first push and pitching idea

parent 0a2d0cbc
No related branches found
No related tags found
No related merge requests found
.idea
venv
Data
\ No newline at end of file
import cv2
def Close(cap):
cap.release()
cv2.destroyAllWindows()
print('closing')
import cv2
import math
def CalculateWidth(size, h, w, imgCropped, imgFixed):
c = size / h
wCal = math.ceil(c * w)
imgResized = cv2.resize(imgCropped, (wCal, size))
wGap = math.ceil((size - wCal) / 2)
imgFixed[:, wGap:wCal + wGap] = imgResized
def CalculateHeight(size, h, w, imgCropped, imgFixed):
c = size / w
hCal = math.ceil(c * h)
imgResized = cv2.resize(imgCropped, (size, hCal))
hGap = math.ceil((size - hCal) / 2)
imgFixed[hGap:hCal + hGap, :] = imgResized
def GetPrediction(classifier, imgFixed):
prediction, index = classifier.getPrediction(imgFixed, draw=False)
return prediction, index
def GetLabels(path):
letters = []
with open(path, 'r') as file:
for line in file:
parts = line.strip().split(' ')
if len(parts) == 2:
letter = parts[1]
letters.append(letter)
return letters
File added
File added
File added
0 A
1 B
2 C
3 D
4 E
\ No newline at end of file
import time
import cv2
import numpy as np
from Func.CloseProgram import Close
from Func.Helpers import CalculateWidth, CalculateHeight
from cvzone.HandTrackingModule import HandDetector
offset = 20
size = 300
folder = "Data/E"
counter = 0
cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1)
if not cap.isOpened():
print("Failed to open video capture.")
exit()
while True:
success, img = cap.read()
if not success:
print('can\'t read image')
break
hands, img = detector.findHands(img)
if hands:
hand = hands[0]
x, y, w, h = hand['bbox']
imgFixed = np.ones((size, size, 3), np.uint8)
imgCropped = img[y - offset:y + h + offset, x - offset:x + w + offset]
aspectRatio = h / w
if aspectRatio > 1:
CalculateWidth(size, h, w, imgCropped, imgFixed)
else:
CalculateHeight(size, h, w, imgCropped, imgFixed)
cv2.imshow('ImgCropped', imgCropped)
cv2.imshow('ImgFixed', imgFixed)
cv2.imshow("img", img)
key = cv2.waitKey(1)
if key == ord('p'):
Close(cap)
break
if key == ord('s'):
counter += 1
cv2.imwrite(f'{folder}/Image_{time.time()}.jpg', imgFixed)
print(counter)
main.py 0 → 100644
import cv2
import numpy as np
from cvzone.HandTrackingModule import HandDetector
from cvzone.ClassificationModule import Classifier
from Func.CloseProgram import Close
from Func.Helpers import *
offset = 20
size = 300
capture = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1)
classifier = Classifier("Model/keras_model.h5", "Model/labels.txt")
letters = GetLabels("Model/labels.txt")
if not capture.isOpened():
print("Failed to open video capture.")
exit()
while True:
success, img = capture.read()
imgOutput = img.copy()
if not success:
print('can\'t read image')
break
hands, img = detector.findHands(img)
if hands:
hand = hands[0]
x, y, w, h = hand['bbox']
imgFixed = np.ones((size, size, 3), np.uint8)
imgCropped = img[y - offset:y + h + offset, x - offset:x + w + offset]
aspectRatio = h / w
if aspectRatio > 1:
CalculateWidth(size, h, w, imgCropped, imgFixed)
prediction, index = GetPrediction(classifier, imgFixed)
else:
CalculateHeight(size, h, w, imgCropped, imgFixed)
prediction, index = GetPrediction(classifier, imgFixed)
print(prediction)
cv2.putText(imgOutput, letters[index], (x, y), cv2.FONT_HERSHEY_TRIPLEX, 2, (255, 0, 255), 2)
cv2.imshow("img", imgOutput)
key = cv2.waitKey(1)
if key == ord('p'):
Close(capture)
break
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment