Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 105 additions & 90 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
---
runme:
id: 01HXQM38H0ZX9BSHH89DJJB5PA
version: v3
---

# Atlantis engine

![1715102710368](example/docs/image/Atlantis.png)

## Thành quả phát triển

* [X] Thiết kế API
* [X] Công khai toàn bộ mã nguồn
* [X] Tạo các code mẫu để áp dụng
* [ ] đang cập nhật tiếp...
- [x] Thiết kế API
- [x] Công khai toàn bộ mã nguồn
- [x] Tạo các code mẫu để áp dụng
- [ ] đang cập nhật tiếp...

## Thử nghiệm

Expand All @@ -25,15 +31,15 @@ Giao tiếp có vai trò vô cùng quan trọng đối với mỗi chúng ta, n

### Công nghệ sử dụng

* **Thị giác máy tính**
- **Thị giác máy tính**

Thị giác máy tính là gì? Thị giác máy tính là một công nghệ mà máy sử dụng để tự động nhận biết và mô tả hình ảnh một cách chính xác và hiệu quả. Ngày nay, các hệ thống máy tính có quyền truy cập vào khối lượng lớn hình ảnh và dữ liệu video bắt nguồn từ hoặc được tạo bằng điện thoại thông minh, camera giao thông, hệ thống bảo mật và các thiết bị khác. Ứng dụng thị giác máy tính sử dụng trí tuệ nhân tạo và máy học (AI/ML) để xử lý những dữ liệu này một cách chính xác nhằm xác định đối tượng và nhận diện khuôn mặt, cũng như phân loại, đề xuất, giám sát và phát hiện.

* **Máy học**
- **Máy học**

Máy học (machine learning) là một lĩnh vực của trí tuệ nhân tạo (AI) mà trong đó máy tính được lập trình để tự động học và cải thiện từ dữ liệu mà nó nhận được. Thay vì chỉ dựa trên các quy tắc cụ thể được lập trình trước, máy học cho phép máy tính "học" thông qua việc phân tích dữ liệu và tìm ra các mẫu, xu hướng hoặc quy luật ẩn trong dữ liệu mà không cần được lập trình trực tiếp.

* **Giao diện chương trình ứng dụng**
- **Giao diện chương trình ứng dụng**

Giao diện chương trình là gì? Giao diện chương trình – Application Programming Interface viết tắt là API là một trung gian phần mềm cho phép hai ứng dụng giao tiếp với nhau, có thể sử dụng cho web-based system, operating system, database system, computer hardware, hoặc software library.

Expand All @@ -43,8 +49,9 @@ Giao diện chương trình là gì? Giao diện chương trình – Application

#### Dưới đây là hướng dẫn train model cơ bản

```python
# collect_imgs.py
```python {"id":"01HXQM38GYX73YFXXRSQ2KJSEJ"}
# engine/train/collect_imgs.py

import os
import cv2
DATA_DIR = './data'
Expand Down Expand Up @@ -74,16 +81,18 @@ for j in range(number_of_classes):
counter += 1
cap.release()
cv2.destroyAllWindows()


```

```python
#create_dataset.py
```python {"id":"01HXQM38GYX73YFXXRSQXQ5PWM"}
# engine/train/create_dataset.py

import os
import pickle

import mediapipe as mp
import cv2
import matplotlib.pyplot as plt
import mediapipe as mp

mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
Expand All @@ -98,74 +107,85 @@ labels = []
for dir_ in os.listdir(DATA_DIR):
for img_path in os.listdir(os.path.join(DATA_DIR, dir_)):
data_aux = []
x_ = []
y_ = []

xs = []
ys = []

img = cv2.imread(os.path.join(DATA_DIR, dir_, img_path))
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

results = hands.process(img_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y

x_.append(x)
y_.append(y)
for _ in range(len(hand_landmarks.landmark)):
xs.append(hand_landmarks.landmark[0].x)
ys.append(hand_landmarks.landmark[0].y)

for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x - min(x_))
data_aux.append(y - min(y_))
for _ in range(len(hand_landmarks.landmark)):
data_aux.append(hand_landmarks.landmark[0].x - min(xs))
data_aux.append(hand_landmarks.landmark[0].y - min(ys))

data.append(data_aux)
labels.append(dir_)

f = open('data.pickle', 'wb')
pickle.dump({'data': data, 'labels': labels}, f)
f.close()
with open('data.pickle', 'wb') as f:
pickle.dump({'data': data, 'labels': labels}, f)


```

```python
#train_classifier.py
```python {"id":"01HXQM38GYX73YFXXRSR0G097A"}
# engine/train/train_classifier.py

import pickle
from os import remove

import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np
from os import remove
data_dict = pickle.load(open('./data.pickle', 'rb'))
from sklearn.model_selection import train_test_split

with open('./data.pickle', 'rb') as f:
data_dict = pickle.load(f)

data = np.asarray(data_dict['data'])
labels = np.asarray(data_dict['labels'])
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, shuffle=True, stratify=labels, random_state=42)

x_train, x_test, y_train, y_test = train_test_split(data,
labels,
test_size=0.2,
shuffle=True,
stratify=labels,
random_state=42)

model = RandomForestClassifier()

model.fit(x_train, y_train)

y_predict = model.predict(x_test)

score = accuracy_score(y_predict, y_test)

print('{}% of samples were classified correctly !'.format(score * 100))
print(f'{score * 100}% of samples were classified correctly !')

f = open('model.p', 'wb')
pickle.dump({'model': model}, f)
f.close()
with open('../model/model.p', 'wb') as f:
pickle.dump({'model': model}, f)

remove('./data.pickle')

```

```python
#inference_classifier.py
```python {"id":"01HXQM38GYX73YFXXRSRYS7KBR"}
# engine/train/inference_classifier.py

import pickle

import cv2
import mediapipe as mp
import numpy as np
import time

model_dict = pickle.load(open('./model.p', 'rb'))
with open('../model/model.p', 'rb') as f:
model_dict = pickle.load(f)
model = model_dict['model']

cap = cv2.VideoCapture(0)
Expand All @@ -174,89 +194,82 @@ mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles

OUTPUT = []
output: list[str] = []

hands = mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.3)
labels_dict = {0:'ok',1:'xin chao',2:'tam biet'}
CHECK_FRAME = 0
labels_dict = {0: 'ok', 1: 'xin chao', 2: 'tam biet'}
detected = False
predicted_character = ''

while True:

data_aux = []
x_ = []
y_ = []
xs = []
ys = []

ret, frame = cap.read()
H, W, _ = frame.shape

frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

results = hands.process(frame_rgb)

if results.multi_hand_landmarks:
detected = True

CHECK_FRAME+=1
if not detected:
continue

if CHECK_FRAME == 1:
CHECK_FRAME = 0
detected = False

for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame, hand_landmarks, mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())

for hand_landmarks in results.multi_hand_landmarks:
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
for hand_landmarks in results.multi_hand_landmarks:
for _ in range(len(hand_landmarks.landmark)):
xs.append(hand_landmarks.landmark[0].x)
ys.append(hand_landmarks.landmark[0].y)

x_.append(x)
y_.append(y)
for _ in range(len(hand_landmarks.landmark)):
data_aux.append(hand_landmarks.landmark[0].x - min(xs))
data_aux.append(hand_landmarks.landmark[0].y - min(ys))

for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x - min(x_))
data_aux.append(y - min(y_))
x1 = int(min(xs) * W) - 10
y1 = int(min(ys) * H) - 10

x1 = int(min(x_) * W) - 10
y1 = int(min(y_) * H) - 10
x2 = int(max(xs) * W) - 10
y2 = int(max(ys) * H) - 10

x2 = int(max(x_) * W) - 10
y2 = int(max(y_) * H) - 10
prediction = model.predict([np.asarray(data_aux)])

prediction = model.predict([np.asarray(data_aux)])
print(prediction)

print(prediction)
predicted_character = labels_dict[int(prediction[0])]

predicted_character = labels_dict[int(prediction[0])]

cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 0), 4)
cv2.putText(frame, predicted_character , (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 0), 3,
cv2.LINE_AA)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 0), 4)
cv2.putText(frame, predicted_character, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 0), 3, cv2.LINE_AA)

if predicted_character != '':
if len(OUTPUT) == 0 or OUTPUT[-1] != predicted_character:
OUTPUT.append(predicted_character)

if len(output) == 0 or output[-1] != predicted_character:
output.append(predicted_character)

cv2.imshow('frame', frame)
if cv2.waitKey(25)==ord('q'):
if cv2.waitKey(25) == ord('q'):
break

print(OUTPUT)
print(output)

cap.release()
cv2.destroyAllWindows()

```

## Giấy phép

```
```md {"id":"01HXQM38GZ1HBF5NNQ999QT1D4"}
MIT License

Copyright (c) 2024 iotran207
Expand All @@ -276,9 +289,11 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION
OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


```

Phần cuối xin cảm ơn team MEDIAPIPE của google vì đã phát triển một framework thật tuyệt vời và [computervisioneng](https://github.com/computervisioneng) đã tạo nên một repo thật tuyệt vời để học hỏi.
2 changes: 1 addition & 1 deletion engine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY ./api /code/api

COPY ./train/model.p /code/api/model.p
COPY ./model/model.p /code/model/model.p

CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8345"]
Binary file modified engine/api/database.sqlite
Binary file not shown.
11 changes: 4 additions & 7 deletions engine/api/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from os import listdir
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from routers.engine import router as engine_router

app = FastAPI()

origins = ['*']

for file in listdir('./routers'):
if file.endswith('.py') and file != '__init__.py':
module = file.replace('.py', '')
exec(f'from routers.{module} import router')
exec(f'app.include_router(router)')
app.include_router(engine_router)

app.add_middleware(
CORSMiddleware,
Expand All @@ -20,6 +16,7 @@
allow_headers=["*"],
)


@app.get("/")
async def home():
return {"message": "Hello World!"}
return {"message": "Hello World!"}
Loading