File size: 7,516 Bytes
d69879c
6989c25
d69879c
 
 
 
e123fec
d69879c
 
 
 
 
 
 
 
 
 
e123fec
 
 
 
 
d69879c
e123fec
 
d69879c
 
 
 
 
 
 
 
 
 
 
 
 
e123fec
 
 
d69879c
 
 
 
 
 
 
e123fec
d69879c
e123fec
 
6989c25
 
 
 
 
 
 
 
 
 
 
d69879c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6989c25
e123fec
6989c25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e123fec
6989c25
 
 
 
 
 
 
 
 
 
 
d69879c
e123fec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d69879c
 
 
 
 
 
 
 
 
 
 
 
 
 
e123fec
 
 
d69879c
 
 
 
 
 
 
 
 
 
 
 
 
 
e123fec
d69879c
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { useState, useEffect, useRef, useCallback, useMemo } from 'react';
import { Download } from 'lucide-react';

import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import { truncateFileName } from './lib/utils';
import { useFaceLandmarkDetection } from './hooks/useFaceLandmarkDetection';
import { About } from './components/About';
import { Spinner } from './components/Spinner';
import { useFacePokeAPI } from './hooks/useFacePokeAPI';
import { Layout } from './layout';
import { useMainStore } from './hooks/useMainStore';

export function App() {
  const error = useMainStore(s => s.error);
  const setError = useMainStore(s => s.setError);
  const imageFile = useMainStore(s => s.imageFile);
  const setImageFile = useMainStore(s => s.setImageFile);
  const isGazingAtCursor = useMainStore(s => s.isGazingAtCursor);
  const setIsGazingAtCursor = useMainStore(s => s.setIsGazingAtCursor);
  const isFollowingCursor = useMainStore(s => s.isFollowingCursor);
  const setIsFollowingCursor = useMainStore(s => s.setIsFollowingCursor);

  const previewImage = useMainStore(s => s.previewImage);
  const status = useMainStore(s => s.status);
  const blendShapes = useMainStore(s => s.blendShapes);

  const {
    isDebugMode,
    setIsDebugMode,
    interruptMessage,
  } = useFacePokeAPI()

  const {
    canvasRefCallback,
    isMediaPipeReady,
    handleMouseDown,
    handleMouseUp,
    handleMouseMove,
    handleTouchStart,
    handleTouchMove,
    handleTouchEnd,
    currentOpacity
  } = useFaceLandmarkDetection()

  // Refs
  const videoRef = useRef<HTMLDivElement>(null);

  // Handle file change
  const handleFileChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
    const files = event.target.files;
    setImageFile(files?.[0] || undefined)
  }, [setImageFile]);

  const handleDownload = useCallback(() => {
    if (previewImage) {
      const link = document.createElement('a');
      link.href = previewImage;
      link.download = 'modified_image.png';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }, [previewImage]);

  const canDisplayBlendShapes = false

  // Display blend shapes
  const displayBlendShapes = useMemo(() => (
      <div className="mt-4">
        <h3 className="text-lg font-semibold mb-2">Blend Shapes</h3>
        <ul className="space-y-1">
          {(blendShapes?.[0]?.categories || []).map((shape, index) => (
            <li key={index} className="flex items-center">
              <span className="w-32 text-sm">{shape.categoryName || shape.displayName}</span>
              <div className="w-full bg-gray-200 rounded-full h-2.5">
                <div
                  className="bg-blue-600 h-2.5 rounded-full"
                  style={{ width: `${shape.score * 100}%` }}
                ></div>
              </div>
              <span className="ml-2 text-sm">{shape.score.toFixed(2)}</span>
            </li>
          ))}
        </ul>
      </div>
  ), [JSON.stringify(blendShapes)])

  // JSX
  return (
    <Layout>
        {error && (
          <Alert variant="destructive">
            <AlertTitle>Error</AlertTitle>
            <AlertDescription>{error}</AlertDescription>
          </Alert>
        )}
        {interruptMessage && (
          <Alert>
            <AlertTitle>Notice</AlertTitle>
            <AlertDescription>{interruptMessage}</AlertDescription>
          </Alert>
        )}
        <div className="mb-5 relative">
          <div className="flex flex-row items-center justify-between w-full">
            <div className="flex items-center space-x-2">
              <div className="flex items-center justify-center">
                <input
                  id="imageInput"
                  type="file"
                  accept="image/*"
                  onChange={handleFileChange}
                  className="hidden"
                  disabled={!isMediaPipeReady}
                />
                <label
                  htmlFor="imageInput"
                  className={`cursor-pointer inline-flex items-center px-3 h-10 border border-transparent text-sm font-medium rounded-md text-white ${
                    isMediaPipeReady ? 'bg-slate-600 hover:bg-slate-500' : 'bg-slate-500 cursor-not-allowed'
                  } focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-slate-500 shadow-xl`}
                >
                  <Spinner />
                  {imageFile ? truncateFileName(imageFile.name, 32) : (isMediaPipeReady ? 'Choose a portrait photo' : 'Initializing...')}
                </label>
              </div>
              {previewImage && (
                <button
                  onClick={handleDownload}
                  className="inline-flex items-center px-3 h-10 border border-transparent text-sm font-medium rounded-md text-white bg-zinc-600 hover:bg-zinc-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-zinc-500 shadow-xl"
                >
                  <Download className="w-4 h-4 mr-2" />
                  Download
                </button>
              )}
            </div>
            {previewImage && <div className="flex items-center space-x-2">
              {/* experimental features, not active yet */}
              {/*
              <label className="mt-4 flex items-center">
                <input
                  type="checkbox"
                  checked={isGazingAtCursor}
                  onChange={(e) => setIsGazingAtCursor(!isGazingAtCursor)}
                  className="mr-2"
                />
                Autotrack eyes
              </label>
              <label className="mt-4 flex items-center">
                <input
                  type="checkbox"
                  checked={isFollowingCursor}
                  onChange={(e) => setIsFollowingCursor(!isFollowingCursor)}
                  className="mr-2"
                />
                Autotrack head
              </label>
              */}
              <label className="mt-4 flex items-center">
                <input
                  type="checkbox"
                  checked={isDebugMode}
                  onChange={(e) => setIsDebugMode(e.target.checked)}
                  className="mr-2"
                />
                Show face markers
              </label>
            </div>}
          </div>
          {previewImage && (
            <div className="mt-5 relative shadow-2xl rounded-xl overflow-hidden">
              <img
                src={previewImage}
                alt="Preview"
                className="w-full"
              />
              <canvas
                ref={canvasRefCallback}
                className="absolute top-0 left-0 w-full h-full select-none"
                onMouseDown={handleMouseDown}
                onMouseUp={handleMouseUp}
                onMouseMove={handleMouseMove}
                onTouchStart={handleTouchStart}
                onTouchMove={handleTouchMove}
                onTouchEnd={handleTouchEnd}
                style={{
                  position: 'absolute',
                  top: 0,
                  left: 0,
                  width: '100%',
                  height: '100%',
                  opacity: isDebugMode ? currentOpacity : 0.0,
                  transition: 'opacity 0.2s ease-in-out'
                }}
              />
            </div>
          )}
          {canDisplayBlendShapes && displayBlendShapes}
        </div>
        <About />
    </Layout>
  );
}