OpenALPR integration for React Native. Provides a camera component that recognizes license plates in real-time. Currently only for iOS.
- iOS 9+
- RN 0.41+
Start by adding the package and linking it.
$ yarn add react-native-openalpr
$ react-native link react-native-openalpror if you are using npm:
$ npm i -S react-native-openalpr
$ react-native link react-native-openalprUnfortunately, the react-native link command is not doing everything that it needs to be doing, so continue on to the project level instructions.
- Add an entry for
NSCameraUsageDescriptionin yourinfo.plistexplaining why your app will use the camera. If you forget to add this, your app will crash!
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...
<key>NSCameraUsageDescription</key>
<string>We use your camera for license plate recognition to make it easier for you to add your vehicle.</string>
</dict>The project needs to be linked against four libraries: leptonica, opencv, tesseract, and openalpr.
- In Xcode, open your project (
.xcodeproj). - Go to
File -> Add Files(orOption + Command + A) - Click the
Optionsbutton on the bottom and tick theCopy items if neededoption. - Add all four frameworks (leptonica, opencv, tesseract, openalpr) from the
node_modules/react-native-openalpr/ios/Frameworks. This should cause the project to add a framework search path to the project's build settings (e.g.$(PROJECT_DIR)/../node_modules/react-native-openalpr/ios/Frameworks). - Ensure that all four frameworks are included in the
Link Binary With Librariesbuild phase by selecting your project in the tray on the left, selecting theBuild Phasestab, then checking that each framework is included in the list ofLink Binary With Libraries.
The alpr library requires a config file (openalpr.conf) and a data folder (runtime_data), both of which are included in the openalpr framework, but must be copied to the application resources:
- Select your project on the project navigator, then, on the main pane, go to
Targets→<Your Project>→Build Phases→Copy Bundle Resources, and click on the+. - Select
Add Other... - Browse into the
openalpr.frameworkbundle, and command-select bothruntime_dataandopenalpr.conf. UnselectCopy items if neededand selectCreate folder references.
Because the OpenCV binary framework release is compiled without bitcode, the other frameworks built by this script are also built without it, which ultimately means your Xcode project also cannot be built with bitcode enabled. Per this message, it sounds like we want this feature disabled for OpenCV anyway.
To disable bitcode in your project:
- In
Build Settings→Build Options, search forEnable Bitcodeand set it toNo.
OpenALPR exposes a camera component (based on react-native-camera) that is optimized to run OpenALPR image processing on a live camera stream. Among other parameters, the camera accepts a callback, onPlateRecognized, for when a plate is recognized.
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
StatusBar,
} from 'react-native';
import Camera from 'react-native-openalpr';
const styles = StyleSheet.create({
container: {
flex: 1,
},
textContainer: {
position: 'absolute',
top: 100,
left: 50,
},
text: {
textAlign: 'center',
fontSize: 20,
},
});
export default class PlateRecognizer extends React.Component {
constructor(props) {
super(props);
this.camera = null;
this.state = {
camera: {
aspect: Camera.constants.Aspect.fill,
},
plate: 'Scan a plate',
};
}
onPlateRecognized = ({ plate, confidence }) => {
if (confidence > 0.9) {
this.setState({
plate,
})
}
}
render() {
return (
<View style={styles.container}>
<Camera
ref={(cam) => {
this.camera = cam;
}}
style={styles.preview}
aspect={this.state.camera.aspect}
captureQuality={Camera.constants.CaptureQuality.medium}
country="us"
onPlateRecognized={this.onPlateRecognized}
plateOutlineColor="#ff0000"
showPlateOutline
torchMode={Camera.constants.TorchMode.off}
touchToFocus
/>
<View style={styles.textContainer}>
<Text style={styles.text}>{this.state.plate}</Text>
</View>
</View>
);
}
}
AppRegistry.registerComponent('PlateRecognizer', () => PlateRecognizer);The aspect ratio of the camera. Can be one of:
Camera.constants.Aspect.stretchCamera.constants.Aspect.fitCamera.constants.Aspect.fill
The resolution at which video frames are captured and analyzed. For completeness, several options are provided. However, it is strongly recommended that you stick with one of the following for the best frame rates and accuracy:
Camera.constants.CaptureQuality.medium(480x360)Camera.constants.CaptureQuality.480p(640x480)
Specifies which OpenALPR config file to load, corresponding to the country whose plates you wish to recognize. Currently supported values are:
aubreufrgbkrmxsgusvn2
This callback receives a hash with keys:
plate, representing the recognized license plate string; andconfidence, OpenALPR's confidence in the result
Hex string specifying the color of the border to draw around the recognized plate. Example: #ff0000 for red.
If true, this draws an outline over the recognized plate
Turns the flashlight on or off. Can be one of:
Camera.constants.TorchMode.onCamera.constants.TorchMode.offCamera.constants.TorchMode.auto
If true, this focuses the camera where the user taps
- This project currently only works with iOS.
- OpenALPR built from OpenALPR-iOS
- Project scaffold based on react-native-camera
