learning_model 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -1,156 +1,4 @@
1
1
  import LearningImage from './learning/image';
2
- <<<<<<< HEAD
3
2
  import LearningMobilenetImage from './learning/mobilenet_image';
4
- =======
5
-
6
- // 프로그레스 바를 표시하는 클래스
7
- class StatusBar {
8
- constructor(private container: HTMLElement) {}
9
- update(status: number, message: string) {
10
- this.container.innerText = `Status: ${status} ${message}`;
11
- }
12
- }
13
-
14
-
15
- async function appRun() {
16
- //////////////////////////////////////////////////////////////////////////////////////////
17
- const learningImage = new LearningImage({
18
- epochs: 30,
19
- batchSize: 32
20
- });
21
- learningImage.onProgress = (progress: number) => {
22
- const element = document.getElementById('progress-bar');
23
- if (element !== null) {
24
- const bar = new StatusBar(element);
25
- bar.update(progress, '%');
26
- }
27
- }
28
- learningImage.onLoss = (loss: number) => {
29
- const element = document.getElementById('loss-bar');
30
- if (element !== null) {
31
- const bar = new StatusBar(element);
32
- bar.update(loss, 'loss');
33
- }
34
- }
35
- //////////////////////////////////////////////////////////////////////////////////////////
36
- //////////////////////////////////////////////////////////////////////////////////////////
37
- //////////////////////////////////////////////////////////////////////////////////////////
38
-
39
- // root UI
40
- const container = document.createElement('div');
41
- container.id = 'root';
42
-
43
- // learning 준비 체크
44
- const learningReadyCheck = () => {
45
- if (!learningImage.ready()) {
46
- window.alert('준비된 데이터가 없습니다.');
47
- return false;
48
- }
49
- if (learningImage.running()) {
50
- window.alert('이미 진행 중입니다.');
51
- return false;
52
- }
53
- return true;
54
- };
55
-
56
- // 이미지 저장 버튼 클릭
57
- async function handleImageButtonClick(label: string) {
58
- // Canvas 생성
59
- const canvas = document.createElement('canvas');
60
- const video = document.createElement('video');
61
-
62
- // 웹캠 활성화
63
- if (navigator.mediaDevices.getUserMedia) {
64
- const stream = await navigator.mediaDevices.getUserMedia({ video: true });
65
- video.srcObject = stream;
66
- }
67
-
68
- // 비디오가 메타데이터 로딩되면 캔버스에 그리고 이미지 캡처
69
- video.addEventListener('loadedmetadata', () => {
70
- video.play(); // 비디오 플레이 시작
71
- });
72
-
73
- // 비디오가 메타데이터 로딩되면 캔버스에 그리고 이미지 캡처
74
- video.addEventListener('play', () => {
75
- canvas.width = 128;
76
- canvas.height = 128;
77
- const context = canvas.getContext('2d');
78
- if (context) {
79
- context.drawImage(video, 0, 0, canvas.width, canvas.height);
80
- learningImage.addData(label, context.getImageData(0, 0, canvas.width, canvas.height));
81
- }
82
- });
83
- container.appendChild(canvas);
84
- }
85
-
86
- // 추론하기 버튼
87
- async function handleInferButtonClick() {
88
- if(!learningReadyCheck()) {
89
- return;
90
- }
91
-
92
- // Canvas 생성
93
- const canvas = document.createElement('canvas');
94
- const video = document.createElement('video');
95
-
96
- // 웹캠 활성화
97
- if (navigator.mediaDevices.getUserMedia) {
98
- const stream = await navigator.mediaDevices.getUserMedia({ video: true });
99
- video.srcObject = stream;
100
- }
101
-
102
- // 비디오가 메타데이터 로딩되면 캔버스에 그리고 이미지 캡처
103
- video.addEventListener('loadedmetadata', () => {
104
- video.play(); // 비디오 플레이 시작
105
- });
106
-
107
- // 비디오가 메타데이터 로딩되면 캔버스에 그리고 이미지 캡처
108
- video.addEventListener('play', () => {
109
- canvas.width = 128;
110
- canvas.height = 128;
111
- const context = canvas.getContext('2d');
112
- if (context) {
113
- context.drawImage(video, 0, 0, canvas.width, canvas.height);
114
- learningImage.infer(context.getImageData(0, 0, canvas.width, canvas.height));
115
- }
116
- });
117
- container.appendChild(canvas);
118
- }
119
-
120
- // Train 학습하기 버튼
121
- async function handleTrainButtonClick() {
122
- if(learningReadyCheck()) {
123
- await learningImage.train();
124
- }
125
- }
126
-
127
- // image button UI
128
- const image1Button = document.createElement('button');
129
- image1Button.textContent = '라벨1 이미지';
130
- image1Button.addEventListener('click', () => handleImageButtonClick('라벨1 이미지'));
131
- container.appendChild(image1Button);
132
-
133
- // image button UI
134
- const image2Button = document.createElement('button');
135
- image2Button.textContent = '라벨2 이미지';
136
- image2Button.addEventListener('click', () => handleImageButtonClick('라벨2 이미지'));
137
- container.appendChild(image2Button);
138
-
139
- // save button UI
140
- const trainButton = document.createElement('button');
141
- trainButton.textContent = '모델 Train';
142
- trainButton.addEventListener('click', handleTrainButtonClick);
143
- container.appendChild(trainButton);
144
- document.body.appendChild(container);
145
-
146
- // image button UI
147
- const inferButton = document.createElement('button');
148
- inferButton.textContent = '예측하기';
149
- inferButton.addEventListener('click', () => handleInferButtonClick());
150
- container.appendChild(inferButton);
151
-
152
-
153
- }
154
- >>>>>>> 68f03c4696aa804e510cdbd6ebf6efd9e8eda5a4
155
3
 
156
4
  export { LearningImage, LearningMobilenetImage };