src/app/components/detect/detect.component.ts
selector | app-detect |
styleUrls | ./detect.component.scss |
templateUrl | ./detect.component.html |
Properties |
Methods |
constructor(data: AzureFaceApiDataService)
|
||||||
Parameters :
|
getHeight |
getHeight()
|
Gets the height of the image.
Returns :
number
The height of the image. |
getPreview | ||||||||
getPreview(file: File)
|
||||||||
Takes in a file and returns the URL of the file.
Parameters :
Returns :
void
None |
getWidth |
getWidth()
|
Get the width of the image.
Returns :
number
The width of the image. |
handleFileInput | ||||||
handleFileInput(event)
|
||||||
Handles the file input event.
Parameters :
Returns :
void
None |
ngOnInit |
ngOnInit()
|
Returns :
void
|
faceApiResponse |
Type : any
|
height |
Type : number
|
nbFaces |
Type : number
|
Default value : 0
|
uploadedFile |
Type : File
|
Default value : null
|
url |
Type : any
|
width |
Type : number
|
import { Component, OnInit } from '@angular/core';
import { AzureFaceApiDataService } from '../services/azure-face-api-data.service';
@Component({
selector: 'app-detect',
templateUrl: './detect.component.html',
styleUrls: ['./detect.component.scss']
})
export class DetectComponent implements OnInit {
nbFaces = 0;
uploadedFile: File = null;
faceApiResponse: any;
url: any;
width: number;
height: number;
constructor(private data: AzureFaceApiDataService) { }
ngOnInit(): void {
}
/**
* Handles the file input event.
* @param {Event} event - the event that triggered this function.
* @returns None
*/
handleFileInput(event) {
this.nbFaces = 0;
this.uploadedFile = event.target.files[0];
this.data.detectFace_File(this.uploadedFile).subscribe(data => {
/**
* Takes in a face api response and stores it in the state.
* @param {object} data - the face api response object.
* @returns None
*/
this.faceApiResponse = { ...data };
},
/**
* Takes in an error and logs it to the console.
* @param {Error} error - the error to log
* @returns None
*/
error => console.log(error));
this.getPreview(this.uploadedFile);
}
/**
* Takes in a file and returns the URL of the file.
* @param {File} file - the file to get the URL of.
* @returns None
*/
getPreview(file: File) {
var reader = new FileReader();
/**
* Reads the file as a data URL and calls the callback with the data URL.
* @param {File} file - the file to read.
* @returns None
*/
reader.readAsDataURL(file);
reader.onload = (event) => {
this.url = (<FileReader>event.target).result;
}
}
/**
* Gets the height of the image.
* @returns {number} The height of the image.
*/
getHeight() {
var image = document.getElementById('pic') as HTMLImageElement; //this will get the actual dimensions of the visible image
this.height = image.naturalHeight;
return this.height;
}
/**
* Get the width of the image.
* @returns {number} The width of the image.
*/
getWidth() {
var image = document.getElementById('pic') as HTMLImageElement;//Get the image element from the page.
/**
* Sets the width of the image to the natural width of the image.
* @param {HTMLImageElement} image - the image to set the width of.
* @returns None
*/
this.width = image.naturalWidth;
return this.width;
}
}
<link
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
rel="stylesheet"
/>
<div class="container">
<div class="only-logo">
<!--ngid is known as Selector such as conditional statement that return true and false : angular language-->
<div *ngIf="url; else elseBlock1">
<div class="wrapper">
<img id="pic" [src]="url" height="400" width="400" /><br />
<div *ngIf="faceApiResponse">
<app-rectangle
[faceApiResponse]="faceApiResponse"
[height]="getHeight()"
[width]="getWidth()"
></app-rectangle>
</div>
</div>
</div>
<!--Assiging the png file at the place of face detection-->
<ng-template #elseBlock1>
<img
src="http://placehold.jp/262626/ffffff/400x400.png?text=Upload%20an%20image%20to%20Detect%20a%20Face&css=%7B%22border-radius%22%3A%2215px%22%7D"
alt="Upload Files"
height="400"
width="400"
/>
</ng-template>
</div>
<!--Designing the Upload a file button -->
<div class="upload-btn-wrapper">
<a
class="btn-grad btn-primary col-2 mx-auto"
role="button"
(click)="uploader.click()"
>
Upload Image
<input
type="file"
accept="image/*"
#uploader
(change)="handleFileInput($event)"
multiple
/>
</a>
</div>
<!--Designing the Response in table file Format -->
<div *ngIf="faceApiResponse">
<app-table id="first" [faceApiResponse]="faceApiResponse"></app-table>
</div>
</div>
./detect.component.scss
@import "~bootstrap/dist/css/bootstrap.css";
@import "~mdb-angular-ui-kit/assets/scss/mdb.scss";
img {
border-radius: 15px;
padding: 1px;
}
.container {
padding-top: 20px;
}
.only-logo {
padding-bottom: 1em;
}
.wrapper {
position: relative;
}
.upload-btn-wrapper {
margin-top: 5px;
}
.upload-btn-wrapper input[type=file] {
font-size: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
body {
text-align: center !important;
}
.btn-grad {
background-image: linear-gradient(to right, #ff76b4 0%, #66f1ff 51%, #ff76b4 100%)
}
.btn-grad {
margin: 10px;
padding: 15px 20px;
text-align: center;
text-transform: uppercase;
transition: 0.5s;
background-size: 200% auto;
color: #262626;
box-shadow: 0 0 20px #ffffff;
border-radius: 10px;
display: block;
}
.btn-grad:hover {
background-position: right center;
/* change the direction of the change here */
color: #fff;
text-decoration: none;
}