# Fix "AttributeError: module 'cv2' has no attribute 'xfeatures2d'" by Moving SIFT to cv2.SIFT_create

Yes. An agent shows that SIFT lives in the main OpenCV package since 4.4, changes the calls, and reruns the extraction to prove the output shape is unchanged.

[Run this on Rush](https://cloud.getrush.ai/run/fix-module-cv2-has-no-attribute-xfeatures2d/opencv-python/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

`cv2.xfeatures2d` is the contrib module. SIFT sat there while its patent was in force, so code from that era calls `cv2.xfeatures2d.SIFT_create()` and requires `opencv-contrib-python`. Since the 4.4 line, SIFT is in the main package as `cv2.SIFT_create`, and a plain `opencv-python` install has no `xfeatures2d` at all, which is the AttributeError in the title. Reproducing the boundary is cheap: on Python 3.8, `opencv-python==4.3.0.38` has neither name, and `4.4.0.46` has `SIFT_create` and still no `xfeatures2d`.

In a research codebase like zjhthu/OANet, the README's requirements line asks for Python 3.6 and `opencv-contrib-python (3.4.0.12)`, a build that no current Python can install, so anyone setting the project up today installs a recent OpenCV and hits the error. The call is in `demo/extract_sift.py`: `ExtractSIFT.__init__` on line 30 does `cv2.xfeatures2d.SIFT_create(nfeatures=num_kp, contrastThreshold=contrastThreshold)`, `run` reads an image with `cv2.imread`, calls `detectAndCompute` and packs each keypoint's `pt`, `size` and `angle` into an N by 4 array, and the `__main__` block walks `<input_path><seq>/images/<glob>` and writes `.bin` files into `keypoints/` and `descriptors/` through `io_util`. `demo/demo.py` imports the same class with `ExtractSIFT(8000)` to draw matches between the two test images that ship in the directory.

The agent runs the script against those two images in a fresh environment with the current `opencv-python` and quotes the traceback from line 42 into line 30. It confirms the replacement on the installed package rather than from memory: `cv2.SIFT_create` exists and accepts the same `nfeatures` and `contrastThreshold` keywords, and `dir(cv2)` shows no `xfeatures2d`. It also checks the other route, since installing `opencv-contrib-python` of the same version brings back `cv2.xfeatures2d.SIFT_create` as an alias, so a one-line requirements change would also make the error disappear. For a script that uses nothing else from contrib the agent prefers the call change, which works with either package.

The edit is one line, and the proof is the rerun. With `cv2.SIFT_create` the script processes both test images and writes the four `.bin` files; the agent reads them back with the repository's own `read_keypoints` and `read_descriptors` and reports the shapes, 2,000 by 4 and 2,000 by 128 for a 2,000-keypoint run, so the downstream matcher sees the same layout it did before. It then grep-checks the rest of the repository for `xfeatures2d`, reports any other contrib call it finds, and adds a `requirements.txt` with the OpenCV it tested against, which the README's single sentence never had.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This project fails with AttributeError: module 'cv2' has no attribute 'xfeatures2d'. In a clean virtual environment install the current opencv-python and numpy, run the feature extraction script on the test images in the repository, and quote the traceback. Find every call into cv2.xfeatures2d and any other contrib-only module. For SIFT, check on the installed package that cv2.SIFT_create exists and accepts the same keyword arguments, and change the calls; for anything else contrib-only, say so and leave it for a follow-up. Also report whether installing opencv-contrib-python of the same version would have made the original call work, so the reviewer sees both options. Rerun the extraction after the change, read the written keypoint and descriptor files back with the repository's own reader functions and report their shapes. Add a requirements file pinning the OpenCV and numpy versions you tested with, and open a pull request with the traceback, the diff and the rerun output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes the SIFT construction in the extraction script and adds a `requirements.txt` naming the OpenCV and numpy builds the agent ran. The description opens with the traceback from the fresh install, then the two facts that explain it: the main package has had `SIFT_create` since 4.4, and the contrib build requested by the README cannot be installed on a current Python.

Below that is the rerun: the command, the progress over the two test images, the four files written, and the shapes read back through `io_util`. If the repository calls other contrib-only functions the description lists them with their file and line and says they are untouched, so the reviewer knows the scope of what was proven.

What a reviewer checks before merging is the version pin. A project that also runs on an older OpenCV for other reasons needs the pin to say at least 4.4, not an exact build, and the description shows the boundary the agent measured so that choice is informed.

## Run it on every new environment that fails on this import

This error appears when the environment is built, not when the code changes: a new machine, a refreshed Docker image, a colleague cloning the repository. Add an automation to the Project with the prompt above and a `github` trigger on `workflow_run.failure` if the repository has a CI job that installs and runs the extraction. The failed workflow is placed after the prompt as the event, and the saved prompt is the only instruction, so a failure with another cause gets a short report and no pull request.

A repository with no CI can use a `schedule` trigger once a month with the same prompt, installing the current OpenCV in a fresh sandbox and running the script on the shipped test images. A run that finds nothing to fix ends without a pull request and says so.

## Why run it on Rush?

The call change is trivial and the proof is not: it needs a clean install, a run on real images, and a read of the output files in the project's own binary format. On a laptop that competes with an existing OpenCV install, often the contrib build that hides the bug. A Rush sandbox starts empty, so what the agent sees is what a new contributor sees, and the session keeps the install, the traceback and the rerun together.

The result also records a decision that otherwise lives in someone's head: contrib or main package. The agent measures the version boundary and tries both routes, and the pull request says which one the project now depends on and why, so the next person who sees `xfeatures2d` in a tutorial does not reintroduce it.

## Frequently asked questions

### Why did this work before and not now?

Because the earlier environment had `opencv-contrib-python`, where `cv2.xfeatures2d` lives. SIFT's patent kept it in contrib until the 4.4 release moved it into the main package in 2020. A fresh `pip install opencv-python` today has `cv2.SIFT_create` and no contrib module.

### Can I just install opencv-contrib-python instead?

Yes. The current contrib build keeps `cv2.xfeatures2d.SIFT_create` as an alias, so the old call works again. The agent reports that, and still changes the call, because `cv2.SIFT_create` works with both packages and the script uses nothing else from contrib.

### What about SURF?

SURF is still patented and stays in contrib, as `cv2.xfeatures2d.SURF_create`, available only in builds compiled with non-free modules, which the PyPI wheels are not. A script that uses SURF needs a different detector or a custom build; the agent lists such calls and does not change them in this pull request.

### Does the new call give the same keypoints?

It is the same algorithm, moved. The agent proves the output layout by reading the written files back and reporting their shapes, and keeps the same `nfeatures` and `contrastThreshold` values. Exact keypoint positions can still differ between OpenCV releases, which is a property of the library version, not of the call.

## More opencv-python jobs

- [Fix "ValueError: not enough values to unpack (expected 3, got 2)" from cv2.findContours](https://getrush.ai/run/fix-findcontours-not-enough-values-to-unpack/opencv-python/)

## More jobs like this: Fix a bug from an alert

- [Fix "AttributeError: module 'emoji' has no attribute 'UNICODE_EMOJI'"](https://getrush.ai/run/fix-module-emoji-has-no-attribute-unicode-emoji/emoji/)
- [Fix Moment's "Value Provided Is Not in a Recognized RFC2822 or ISO Format" Warning](https://getrush.ai/run/fix-moment-value-provided-is-not-in-a-recognized-format-warning/moment/)
- [Fix "Model.find() no longer accepts a callback" After a Mongoose Upgrade](https://getrush.ai/run/fix-mongoose-callback-error/mongoose/)
- [Fix "DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7"](https://getrush.ai/run/fix-mongoose-strictquery-deprecation-warning/mongoose/)
- [Fix "MongoParseError: options usecreateindex, usefindandmodify are not supported" in Mongoose 6 and Later](https://getrush.ai/run/fix-mongoose-usecreateindex-not-supported/mongoose/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
