summaryrefslogtreecommitdiff
path: root/client/faceSearch/faceSearch.result.js
blob: d63f3265c104d66c42391572f7e34653c4ad5e75 (plain)
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
import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import { courtesyS } from '../util'

import * as actions from './faceSearch.actions'
import { Loader } from '../common'

const errors = {
  bbox: (
    <div>
      <h2>No face found</h2>
      {"Sorry, we didn't detect a face in that image. "}
      {"Please choose an image where the face is large and clear."}
    </div>
  ),
  nomatch: (
    <div>
      <h2>{"You're clear"}</h2>
      {"No images in this dataset match your face.  We show only matches above 70% probability."}
    </div>
  ),
  error: (
    <div>
      <h2>{"No matches found"}</h2>
      {"Sorry, an error occured."}
    </div>
  ),
}

class FaceSearchResult extends Component {
  render() {
    const { dataset } = this.props.payload
    const { query, distances, results, loading, error } = this.props.result
    if (loading) {
      return (
        <div className='result'>
          <div>
            <Loader /><br />
            <h2>Searching...</h2>
          </div>
        </div>
      )
    }
    if (error) {
      console.log(error)
      let errorMessage = errors[error] || errors.error
      return (
        <div className='result'>{errorMessage}</div>
      )
    }
    if (!results) {
      return <div className='result'></div>
    }
    if (!results.length) {
      return (
        <div className='result'>{errors.nomatch}</div>
      )
    }
    const els = results.map((result, i) => {
      const distance = distances[i]
      const { uuid } = result.uuid
      const { fullname, gender, description, images } = result.identity
      return (
        <div key={i}>
          <img src={'https://megapixels.nyc3.digitaloceanspaces.com/v1/media/' + dataset + '/' + uuid + '.jpg'} />
          {fullname} {'('}{gender}{')'}<br/>
          {description}<br/>
          {courtesyS(images, 'image')}{' in dataset'}<br />
          {Math.round((1 - distance) * 100)}{'% match'}
        </div>
      )
    })

    return (
      <div className='result'>
        <div className="about">
          <h2>Did we find you?</h2>
          {'These faces matched images in the '}
          <b><tt>{dataset}</tt></b>
          {' dataset with over 70% probability.'}
          <br />
          <small>Query took {query.timing.toFixed(2)} seconds</small>
        </div>
        <div className='results'>
          {els}
        </div>
      </div>
    )
  }
}

const mapStateToProps = state => ({
  query: state.faceSearch.query,
  result: state.faceSearch.result,
  options: state.faceSearch.options,
})

const mapDispatchToProps = dispatch => ({
  actions: bindActionCreators({ ...actions }, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(FaceSearchResult)