Activity #3020
closedFeature #2955: Design and Development
Create a new mode to integrate an AI service
100%
Description
python AI service to return annotation cordinates.
The image data to input to service by reading dicom tags.
To create a new mode in the OHIF (Open Health Imaging Foundation) Viewer that calls a Python service to retrieve annotation coordinates and overlay them on the image:
1. Set Up the Python Service:
- Ensure you have a Python service that can return annotation coordinates. This service should accept an image identifier (or relevant data) and return the coordinates for annotations in a format that can be used by OHIF.
2. Modify OHIF Viewer:
- You need to create a custom mode in the OHIF Viewer. OHIF modes are essentially configurations that define how the viewer behaves and what tools are available.
3. Integrate the Python Service Call:
- Make an API call from the OHIF Viewer to the Python service to fetch the annotation coordinates.
- Overlay the received annotation coordinates on the image.
- Detailed Steps:
- 1. Python Service Setup
Ensure your Python service is running and capable of returning annotation coordinates. Here is a simple example using Flask:
```python
from flask import Flask, request, jsonify
app = Flask(name)
@app.route('/get_annotations', methods=['POST'])
def get_annotations():
data = request.json
image_id = data['image_id']
# Dummy coordinates, replace with actual logic
annotations = [
{'x': 100, 'y': 150, 'width': 50, 'height': 50},
{'x': 200, 'y': 250, 'width': 60, 'height': 60},
]
return jsonify(annotations)
if name == '__main__':
app.run(debug=True)
```
- 2. Create a New Mode in OHIF Viewer
1. Clone the OHIF Viewer Repository:
```
git clone https://github.com/OHIF/Viewers.git
cd Viewers
```
2. Create a Custom Mode:
In the `platform/viewer/src/modes` directory, create a new mode file, for example, `customAnnotationsMode.js`.
3. Define the Custom Mode:
```javascript
// customAnnotationsMode.js
import { id } from './id';
import { OHIF } from '@ohif/core';
const mode = {
id: 'customAnnotations',
routeName: 'custom',
displayName: 'Custom Annotations Mode',
onModeEnter: ({ servicesManager, extensionManager }) => {
// Set up necessary tools and extensions here
},
onModeExit: () => {
// Clean up if needed
},
// Custom behavior here
sopClassHandlers: [
{
sopClassUids: [],
getDisplaySetsFromSeries: (series, study, servicesManager) => {
// Fetch annotations from the Python service
const annotations = fetchAnnotations(series);
// Overlay annotations
overlayAnnotations(annotations);
return series;
},
},
],
};
async function fetchAnnotations(series) {
const response = await fetch('http://localhost:5000/get_annotations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ image_id: series.imageId }),
});
const annotations = await response.json();
return annotations;
}
function overlayAnnotations(annotations) {
annotations.forEach(annotation => {
// Logic to overlay annotations on the viewer
// Example:
console.log(`Annotation: ${JSON.stringify(annotation)}`);
});
}
OHIF.extensionManager.registerMode(mode);
export default mode;
```
4. Register the New Mode:
Update `platform/viewer/src/index.js` to include the new mode:
```javascript
import customAnnotationsMode from './modes/customAnnotationsMode';
const appConfig = {
modes: [customAnnotationsMode, ...otherModes],
// Other configuration
};
OHIF.init(appConfig);
```
5. Build and Run the Viewer:
Follow the build instructions in the OHIF repository to run your customized viewer.
- 3. Deploy and Test
1. Deploy the Python service and OHIF Viewer:
- Ensure your Python service is running and accessible.
- Run the customized OHIF Viewer and navigate to your custom mode.
2. Test the Integration:
- Load an image in the OHIF Viewer.
- Verify that the viewer makes a call to the Python service and overlays the returned annotation coordinates on the image.