|
| 1 | +//import { Button } from 'reactstrap'; |
| 2 | +//import React from 'react'; |
| 3 | +const Button = window.Reactstrap.Button; |
| 4 | + |
| 5 | + |
| 6 | +const Collapse = window.Reactstrap.Collapse; |
| 7 | +const Navbar = window.Reactstrap.Navbar; |
| 8 | +const NavbarBrand = window.Reactstrap.NavbarBrand; |
| 9 | +const Nav = window.Reactstrap.Nav; |
| 10 | +const NavItem = window.Reactstrap.NavItem; |
| 11 | +const NavLink = window.Reactstrap.NavLink; |
| 12 | + |
| 13 | + |
| 14 | +const Router = window.ReactRouterDOM.BrowserRouter; |
| 15 | +const Route = window.ReactRouterDOM.Route; |
| 16 | +const ReactMarkdown = window.ReactMarkdown; |
| 17 | + |
| 18 | +const Form = window.Reactstrap.Form; |
| 19 | +const FormGroup = window.Reactstrap.FormGroup; |
| 20 | +const Label = window.Reactstrap.Label; |
| 21 | +const Input = window.Reactstrap.Input; |
| 22 | + |
| 23 | + |
| 24 | +const UncontrolledDropdown = window.Reactstrap.UncontrolledDropdown; |
| 25 | +const Dropdown = window.Reactstrap.Dropdown; |
| 26 | +const DropdownToggle = window.Reactstrap.DropdownToggle; |
| 27 | +const DropdownMenu = window.Reactstrap.DropdownMenu; |
| 28 | +const DropdownItem = window.Reactstrap.DropdownItem; |
| 29 | +const Spinner = window.Reactstrap.Spinner; |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +const axios = window.axios; |
| 34 | + |
| 35 | +const Select = window.Select; |
| 36 | + |
| 37 | + |
| 38 | +//import { Button } from 'reactstrap'; |
| 39 | + |
| 40 | +// Obtain the root |
| 41 | +const rootElement = document.getElementById('root'); |
| 42 | + |
| 43 | + |
| 44 | +class About extends React.Component { |
| 45 | + // |
| 46 | + |
| 47 | +// Use the render function to return JSX component |
| 48 | + render() { |
| 49 | + return ( |
| 50 | + |
| 51 | + <div> |
| 52 | + <h1>About</h1> |
| 53 | + <ReactMarkdown source={window.APP_CONFIG.about}/> |
| 54 | + </div> |
| 55 | + ); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +// Create a ES6 class component |
| 61 | +class MainPage extends React.Component { |
| 62 | + // |
| 63 | + |
| 64 | + constructor(props) { |
| 65 | + super(props); |
| 66 | + this.state = { |
| 67 | + file: null, |
| 68 | + predictions: [], |
| 69 | + imageSelected: false, |
| 70 | + url: null, |
| 71 | + isLoading: false, |
| 72 | + selectedOption: null, |
| 73 | + |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + _onFileUpload = (event) => { |
| 78 | + this.setState({ |
| 79 | + rawFile: event.target.files[0], |
| 80 | + file: URL.createObjectURL(event.target.files[0]), |
| 81 | + imageSelected: true |
| 82 | + }) |
| 83 | + }; |
| 84 | + |
| 85 | + _onUrlChange = (url) => { |
| 86 | + this.state.url = url; |
| 87 | + if ((url.length > 5) && (url.indexOf("http") === 0)) { |
| 88 | + this.setState({ |
| 89 | + file: url, |
| 90 | + imageSelected: true |
| 91 | + }) |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + _clear = async (event) => { |
| 96 | + this.setState({ |
| 97 | + file: null, |
| 98 | + imageSelected: false, |
| 99 | + predictions: [], |
| 100 | + rawFile: null, |
| 101 | + url: "" |
| 102 | + }) |
| 103 | + }; |
| 104 | + |
| 105 | + _predict = async (event) => { |
| 106 | + this.setState({isLoading: true}); |
| 107 | + |
| 108 | + let resPromise = null; |
| 109 | + if (this.state.rawFile) { |
| 110 | + const data = new FormData(); |
| 111 | + data.append('file', this.state.rawFile); |
| 112 | + resPromise = axios.post('/api/classify', data); |
| 113 | + } else { |
| 114 | + resPromise = axios.get('/api/classify', { |
| 115 | + params: { |
| 116 | + url: this.state.file |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + try { |
| 122 | + const res = await resPromise; |
| 123 | + const payload = res.data; |
| 124 | + |
| 125 | + this.setState({predictions: payload.predictions, isLoading: false}); |
| 126 | + console.log(payload) |
| 127 | + } catch (e) { |
| 128 | + alert(e) |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + |
| 133 | + renderPrediction() { |
| 134 | + |
| 135 | + const predictions = this.state.predictions || []; |
| 136 | + |
| 137 | + if (predictions.length > 0) { |
| 138 | + |
| 139 | + const predictionItems = predictions.map((item) => |
| 140 | + <li>{item.class} ({item.prob * 100}%) </li> |
| 141 | + ); |
| 142 | + |
| 143 | + return ( |
| 144 | + <ul> |
| 145 | + {predictionItems} |
| 146 | + </ul> |
| 147 | + ) |
| 148 | + |
| 149 | + } else { |
| 150 | + return null |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + handleChange = (selectedOption) => { |
| 155 | + this.setState({selectedOption}); |
| 156 | + console.log(`Option selected:`, selectedOption); |
| 157 | + }; |
| 158 | + |
| 159 | + sampleUrlSelected = (item) => { |
| 160 | + this._onUrlChange(item.url); |
| 161 | + }; |
| 162 | + render() { |
| 163 | + const sampleImages = APP_CONFIG.sampleImages; |
| 164 | + return ( |
| 165 | + <div> |
| 166 | + <h2>{APP_CONFIG.description}</h2> |
| 167 | + |
| 168 | + <p>Select an image </p> |
| 169 | + |
| 170 | + <Form> |
| 171 | + <FormGroup> |
| 172 | + <div> |
| 173 | + <p>Provide a Url</p> |
| 174 | + <div> |
| 175 | + |
| 176 | + <UncontrolledDropdown > |
| 177 | + <DropdownToggle caret> |
| 178 | + Sample Image Url |
| 179 | + </DropdownToggle> |
| 180 | + <DropdownMenu> |
| 181 | + {sampleImages.map(si => |
| 182 | + <DropdownItem onClick={()=>this.sampleUrlSelected(si)}> |
| 183 | + {si.name} |
| 184 | + </DropdownItem>) |
| 185 | + } |
| 186 | + |
| 187 | + </DropdownMenu> |
| 188 | + </UncontrolledDropdown> |
| 189 | + |
| 190 | + </div> |
| 191 | + <Input value={this.state.url} name="file" onChange={(e)=>this._onUrlChange(e.target.value)} |
| 192 | + /> |
| 193 | + </div> |
| 194 | + </FormGroup> |
| 195 | + |
| 196 | + <h3>OR</h3> |
| 197 | + <FormGroup id={"upload_button"}> |
| 198 | + <div> |
| 199 | + <p>Upload an image</p> |
| 200 | + </div> |
| 201 | + <Label for="imageUpload"> |
| 202 | + <Input type="file" name="file" id="imageUpload" accept=".png, .jpg, .jpeg" ref="file" |
| 203 | + onChange={this._onFileUpload}/> |
| 204 | + <span className="btn btn-primary">Upload</span> |
| 205 | + </Label> |
| 206 | + </FormGroup> |
| 207 | + |
| 208 | + <img src={this.state.file} className={"img-preview"} hidden={!this.state.imageSelected}/> |
| 209 | + |
| 210 | + <FormGroup> |
| 211 | + <Button color="success" onClick={this._predict} |
| 212 | + disabled={this.state.isLoading}> Predict</Button> |
| 213 | + <span className="p-1 "/> |
| 214 | + <Button color="danger" onClick={this._clear}> Clear</Button> |
| 215 | + </FormGroup> |
| 216 | + |
| 217 | + |
| 218 | + {this.state.isLoading && ( |
| 219 | + <div> |
| 220 | + <Spinner color="primary" type="grow" style={{width: '5rem', height: '5rem'}}/> |
| 221 | + |
| 222 | + </div> |
| 223 | + )} |
| 224 | + |
| 225 | + </Form> |
| 226 | + |
| 227 | + {this.renderPrediction()} |
| 228 | + |
| 229 | + |
| 230 | + </div> |
| 231 | + ); |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +class CustomNavBar extends React.Component { |
| 236 | + |
| 237 | + |
| 238 | + render() { |
| 239 | + const link = APP_CONFIG.code; |
| 240 | + return ( |
| 241 | + <Navbar color="light" light fixed expand="md"> |
| 242 | + <NavbarBrand href="/">{'Image Classifier'}</NavbarBrand> |
| 243 | + <Collapse navbar> |
| 244 | + <Nav className="ml-auto" navbar> |
| 245 | + |
| 246 | + </Nav> |
| 247 | + </Collapse> |
| 248 | + </Navbar> |
| 249 | + ) |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +// Create a function to wrap up your component |
| 254 | +function App() { |
| 255 | + return ( |
| 256 | + |
| 257 | + |
| 258 | + <Router> |
| 259 | + <div className="App"> |
| 260 | + <CustomNavBar/> |
| 261 | + <div> |
| 262 | + <main role="main" className="container"> |
| 263 | + <Route exact path="/" component={MainPage}/> |
| 264 | + <Route exact path="/about" component={About}/> |
| 265 | + |
| 266 | + </main> |
| 267 | + |
| 268 | + |
| 269 | + </div> |
| 270 | + </div> |
| 271 | + </Router> |
| 272 | + ) |
| 273 | +} |
| 274 | + |
| 275 | +(async () => { |
| 276 | + const response = await fetch('/config'); |
| 277 | + const body = await response.json(); |
| 278 | + |
| 279 | + window.APP_CONFIG = body; |
| 280 | + |
| 281 | + // Use the ReactDOM.render to show your component on the browser |
| 282 | + ReactDOM.render( |
| 283 | + <App/>, |
| 284 | + rootElement |
| 285 | + ) |
| 286 | +})(); |
| 287 | + |
0 commit comments